use function to add select in options in Hamlet

Skip to first unread message

kees Bleijenberg

Dec 17, 2021, 8:09:52 AM12/17/21
to Yesod Web Framework
This is what I do for generating options in a select in Hamlet:

-- as is [0,1,2,3] :: [Int]
-- isOne is a function from Int -> Bool
   $forall a <- as
      $if isOne a
         <option value=#{show a} selected> #{show a}  (1)
         <option value=#{show a}> #{show a}           (2)    
This works. I get a select with 1 selected.

When I create a option almost always line 1 is equal to line 2, but the first line contains the word "selected"

Idea: I write the function
selected :: Bool -> Text
selected True = "selected"
selected False = ""

and change the code to
   $forall a <- as
      <option value=#{show a} #{selected $ isOne a}> #{show a}
Unfortunately, now the generated Html is:
<select><option value="0" id="{selected" $ isOne a}> 0</option>
<option value="1" id="{selected" $ isOne a}> 1</option> ....
Not what I want.

What is going wrong en how can I do this better?


Dec 17, 2021, 8:51:48 AM12/17/21
to Yesod Web Framework
There is a special syntax in Hamlet for this:

<option value=#{show a} :isOne a:selected> #{show a}

There are also some types of selectField which returns a Field which has a FieldViewFunc which returns a widget that you can embed in your hamlet. Not sure if it's easy to use but the code for <select> is already there.

kees Bleijenberg

Dec 18, 2021, 5:28:01 AM12/18/21
to Yesod Web Framework
Thank you.This solves the problem.
Reply all
Reply to author
0 new messages