问题
I can't figure out the routing strangeness in Snap 0.9 (and, most likely, other versions)
I understand "/" is a catch everything pattern unless I put ifTop function inside the handler, right? So, with ("/", blah) route, any URL should be handled by the blah handler, correct?
Playing with the default app generated by snap init, I can't get snap to render anything but index.tpl for the root request.
Given root ("/", blah) and handler
blah :: Handler App App ()
blah = render "blah"
it renders blah template for any URL but the root URL! So "/anything" renders blah template but "/" renders index template.
Changing blah handler to
blah = ifTop $ render "blah"
does not change the behavior whatsoever. Only in this case I cant route to "/anything". Routing to "/" still renders index template. What am I missing? How can I get "/" to render template of my choice?
回答1:
If you have "index.tpl" template, then it will be used to render root page anyway. The issue is heistInit
function:
heistInit :: FilePath -- ^ Path to templates
-> SnapletInit b (Heist b)
heistInit templateDir = do
makeSnaplet "heist" "" Nothing $ do
hs <- heistInitWorker templateDir defaultHeistState
addRoutes [ ("", heistServe) ]
return hs
You see that it calles addRoutes
. So, if you call heistInit
before adding your own routes (default template does that), then your handlers will not have even a chance if there is a template with the same name (or index.tpl for root).
So just place addRoutes routes
above heistInit
(app
in Site.hs in default template).
来源:https://stackoverflow.com/questions/11114781/snap-0-9-routing-behavior