Sitewide variables in Yesod layout (django context

2019-06-26 05:06发布

问题:

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.



标签: haskell yesod