haskellhaskell-snap-frameworkheist

How do I render a heist template with splices?


I'm trying to use renderWithSplices - http://hackage.haskell.org/package/snap-1.0.0.0/docs/Snap-Snaplet-Heist.html#v:renderWithSplices

What is an idiomatic way to get a value of type Splices s (for the second parameter to renderWithSplices)?


Solution

  • The heist tutorial has a couple examples of defining the Splices type.

    If you click on the Splices link in the haddock you linked to above, we see that Splices is defined as follows:

    type Splices s = MapSyntax Text s
    

    If you then click on the MapSyntax link, you're taking to the docs for the map-syntax package. This is a small package defining some more convenient syntax for creating maps. There's an example at the top, but the general pattern is as follows:

    mySplices = do
        k1 ## v1
        k2 ## v2
    

    Since the previous definition told us that the keys are text, we can do the following to find out what the type of the values needs to be:

    someFunc = do
        ...
        renderWithSplices "my-template" mySplices
        ...
    
    mySplices = do
        "foo" ## I.textSplice "bar"
    

    If you have a recent enough GHC (I think 7.8 or higher), the underscore on that last line will create a type hole and cause the compiler to tell you what the type of the thing in that location should be. Then you can write the appropriate thing of that type and it should be more obvious what to do from there.