Sitewide variables in Yesod layout (django context processors analog)

强颜欢笑 提交于 2019-12-10 17:29:44

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/11148074/sitewide-variables-in-yesod-layout-django-context-processors-analog

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