Adding OnLoad hooks for Heist templates using the Snap Web Framework

169 Views Asked by At

I wish to add some hooks for pre-processing Heist templates in a web application using Snap. My naïve approach so far has been to use the following code in my application initializer:

app :: SnapletInit App App
app = makeSnaplet "app" "The main web application" Nothing $ do
    -- ...
    h <- nestSnaplet "heist" heist $ heistInit "resources/templates"
    -- ...
    modifyHeistTS . addOnLoadHook $ myTemplateHook

This code does of course not work, since the hook is added after the templates are loaded from "resources/templates"

How would one go about modifying the Heist template state before the initial templates are loaded, in order to add OnLoad hooks?

1

There are 1 best solutions below

1
On BEST ANSWER

Excellent question. It looks like this is an oversight on my part when designing the library. I'll think about how best to support this and get back to you.

If you want an immediate workaround, insert something to add your hook to the template state that gets passed into loadTemplates here. Or alternatively, you could modify emptyTemplateState directly in Heist.

Update: I added a heistInit' function that should take care of this issue. It'll be in the next hackage release. Until then, you can get it from github.