Text.Blaze has an operator ! to add attributes to html:
option ! id "bla" ! value "1" ! selected "" $ "Hello!"
My question is how can i make attributes optional ?
Right now my code is ugly:
option ! id "bla" ! value "1" ! (if x == val then selected "" else someStupidAttribute "") $ "Hello!"
This leads to every html option element to have unnecessary irrelevant attribute just because i have to supply one.
EDIT: I accepted hammar’s answer. I created a helper function:
(!?) :: Attributable h => h -> (Bool,Attribute) -> h
html !? (True, attr) = html ! attr
html !? _ = html
And here’s how to use it:
option ! id "bla" ! value "1" !? ((k == val), selected "") $ "Hello!"
How about defining a convenience operator to apply attributes conditionally?
With this, you can write your example as follows.
Here,
toMaybeis just a useful helper for buildingMaybevalues, but you can use something else if you wish.