Yesod布局中的站点范围变量(django上下文处理器模拟) [英] Sitewide variables in Yesod layout (django context processors analog)

查看:126
本文介绍了Yesod布局中的站点范围变量(django上下文处理器模拟)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Yesod有可能在全网范围内有变数吗?假设,我有主菜单哪些条目从数据库中收集,并在每个页面上呈现:我应该在每个处理程序中手动获取条目?



我试过做这在Foundation.hs中实例Yesod应用 defaultLayout 中的声明:

  instance Yesod App其中
...

defaultLayout小部件= do
...
条目< - runDB $ selectList [RubricOnTop ==。 True] []
...

但是这样编译失败:我有一个类型不匹配实体 RubricGeneric



解决方案

  $ forall rubric<  -  map entityVal rubrics 
...
code>

在default-layout-wrapper.hamlet中。

解决方案

您显示的代码很好,在使用条目时,需要记住类型是实体专栏,而不是专栏


Is there a possibility to have a sitewide variables in Yesod? Suppose, I have main menu which entries are gathered from DB and that is rendered on every page: should i fetch entries manually in each handler?

I've tried to do this in Foundation.hs in instance Yesod App declaration in defaultLayout:

    instance Yesod App where
    ...

    defaultLayout widget = do
      ...
      entries <- runDB $ selectList [RubricOnTop ==. True] []
      ...

But this fails to compile: i have a type mismatch Entity with RubricGeneric.

Solution

    $forall rubric <- map entityVal rubrics
      ...

in default-layout-wrapper.hamlet. Haven't found solution to do this in defaultLayout function yet.

解决方案

The code you've shown is fine, but you'll need to keep in mind when using entries that the type is Entity Rubric, not Rubric.

这篇关于Yesod布局中的站点范围变量(django上下文处理器模拟)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆