Snap 0.9路由行为

编程入门 行业动态 更新时间:2024-10-27 14:32:18
本文介绍了Snap 0.9路由行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法弄清楚Snap 0.9中的路由奇怪(很可能还有其他版本)。我明白/是一种捕获所有模式的东西,除非我把ifTop函数放在处理程序中,对吗? 因此,使用(/,blah)路由,任何URL都应该由blah处理程序处理,是正确的吗?

使用由snap init,我不能得到快照来渲染除root.tpl之外的任何内容以获取根请求。

给定root(/,blah)和handler

blah :: Handler App App() blah = renderblah

它会为任何网址提供blah模板,但会显示根网址! 所以/ anything呈现blah模板,但/呈现索引模板。

将blah处理程序更改为

blah = ifTop $ renderblah

不会改变任何行为。 只有在这种情况下,我不能路由到/任何东西。路由到/仍呈现索引模板。 我错过了什么?如何获取/来呈现我选择的模板?

无论如何,它将用于呈现根页面。问题是 heistInit 函数:

heistInit :: FilePath - ^模板路径 - > SnapletInit b(Heist b) heistInit templateDir = do makeSnapletheistNothing $ do hs < - heistInitWorker templateDir defaultHeistState addRoutes [(,heistServe) ] return hs

您会发现它调用 addRoutes 。所以,如果你在添加你自己的路由之前调用 heistInit (默认模板确实如此),那么你的处理程序将不会有机会一个名字相同的模板(或者index.tpl的根目录)。

因此,只需将 addRoutes routes 放在 heistInit ( app 在默认模板中的Site.hs中)。

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?

解决方案

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

更多推荐

Snap 0.9路由行为

本文发布于:2023-11-12 13:48:28,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   Snap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!