Snap 0.9 routing behavior

谁说胖子不能爱 提交于 2019-12-07 15:14:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!