Skip to content

How to query database inside an view? #600

Answered by mpscholten
s0kil asked this question in Q&A
Discussion options

You must be logged in to vote

You could write a controller helper that stores the navigation items inside the controller context and then hook it into the beforeAction like this:

fetchNavigation = do
    categories :: [Category] <- query @Category |> fetch
    putContext categories

-- Inside the controller file
instance Controller MyController where
    beforeAction = do
        fetchNavigation

Now access this inside the view like this:

renderNavigation :: Html
renderNavigation = [hsx|{categories}|]
    where
        categories = fromFrozenContext @[Category]

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by s0kil
Comment options

s0kil
Dec 13, 2020
Collaborator Author

You must be logged in to vote
1 reply
@mpscholten
Comment options

Comment options

s0kil
Dec 15, 2020
Collaborator Author

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants