Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

5. Widgets > Generate IDs

Generate IDs

If we’re really going for true code reuse here, we’re eventually going to run into name conflicts. Let’s say that there are two helper libraries that both use the class name “foo” to affect styling. We want to avoid such a possibility. Therefore, we have the newIdent function. This function automatically generates a word that is unique for this handler.

Example 5-2. Using newIdent

getRootR = defaultLayout $ do
    headerClass <- lift newIdent
    toWidget [hamlet|<h1 .#{headerClass}>My Header|]
    toWidget [lucius| .#{headerClass} { color: green; } |]

Note

You might be wondering: what does lift mean? A Widget is a monad transformer, sitting on top of a Handler. newIdent is a function of a Handler, so we need to “lift” the function from the Handler layer to the Widget layer to use it. We can actually use this same approach to perform complex actions, like database queries, from within a widget. We’ll cover that when we discuss Yesod’s monads.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial