Question

What is the better way to generate HTML tag attributes by condition. I have an external function in the module, which can give me some value (Bool for example). And based on that I need or don't need add a class to the tag. this element has children.

<tr *class should be here*>
  <td>
  ...
Was it helpful?

Solution

Since Hamlet allows to apply functions within an interpolation, you can simply use the function. Sample demo:

{-# LANGUAGE QuasiQuotes #-}

import Text.Hamlet (shamlet)
import Text.Blaze.Html.Renderer.String (renderHtml)

myClass :: Bool -> String
myClass _ = "dummyClass"

main :: IO ()
main = putStrLn $ renderHtml [shamlet|
 <tr .#{myClass True}>
   <td> dummy text |]

In ghci:

λ> main
<tr class="dummyClass"><td> dummy text </td>
</tr>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top