Yesod: Is it possible to to iterate a haskell list in Julius?

655 views Asked by At

I have a list of coordinates that I need to put on map. Is it possible in julius to iterate the list ? Right now I am creating an hidden table in hamlet and accessing that table in julius which does not seem to be an ideal solution. Could some one point to a better solution ? Thanks.

edit: Passing a JSON string for the list (which can be read by julius) seems to solve my problem.

1

There are 1 answers

0
Judah Jacobson On

As far as I know, you can't directly iterate over a list in julius. However, you can use the Monoid instance for the Javascript type to accomplish a similar effect. For example:

import Text.Julius
import Data.Monoid
rows :: [Int] -> t -> Javascript
rows xs = mconcat $ map row xs
  where
    row x = [julius|v[#{show x}] = #{show x};
|]

Then you can use rows xs wherever you'd normally put a julius block. For example, in ghci:

> renderJavascript $ rows [1..5] ()
"v[1] = 1;\nv[2] = 2;\nv[3] = 3;\nv[4] = 4;\nv[5] = 5;\n"