I'm trying to render a template using Snap and Heist.
I'm sure my handler function is called correctly (if I replace handler function's content with undefined
, it fails as expected. Debug.Trace.trace
also works as expected).
This handler function consists of one line: render "template"
. But for some reason I'm getting No handler accepted <url>
error instead of a template not found
or something like that.
I think the problem here is that I'm placing my template in wrong directory, but there's no way to know where are searched for templates. So my question is:
- Isn't this error message misleading? It should have been something like
template not found: template.tpl
- Where can I know which directories are searched for templates?
I think the snap application created by snap init
is the problem. I only made slight modifications on it:
- I added one more field to
App
record:_myapp :: Snaplet Myapp
- In
app
initializer function, I added:n <- embedSnaplet "myapp" myapp myappInit
and then passed n to record. - I created new file
src/Myapp.hs
.
Here are relevant parts in Myapp.hs
:
myappInit = do
...
h <- nestSnaplet "" heist $ hesitInit "myapp_templates"
addRoutes routes
...
routes = [ ("/submit", submitHandler) ]
submitHandler = trace "rendering submit" $ render "submit"
But for some reason even though I see rendering submit
printed to console when I go to http://0.0.0.0:8000/myapp/submit
, I get No handler accepted "/hsnews/submit"
message as HTTP response (instead of rendered template). I have submit.tpl
and _submit.tpl
in snaplets/heist/myapp_templates
.
Attic's answer is very good, but didn't solve my problem. This is because of a difference between
nestSnaplet
andembedSnaplet
.I don't think
embedSnaplet
's documentation mention this difference:but there is also this difference, in
embedSnaplet
, child snaplet has a new root directory insnaplets/embeddedSnapletName
and all children snaplets of the embedded snaplet will usesnaplets/embeddedSnapletName
folder as root.So I had to put my
templates
folder tosnaplets/myapp/templates
and that solved it.