简体   繁体   中英

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 )?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM