-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return empty field when Listfield has no elements to allow using ()$ … #348
base: master
Are you sure you want to change the base?
Conversation
…around a ()$ block.
This solution has several problems:
A better solution would be something like A better choice might be implementing functions over lists in templates. But a change to the definition of The simplest solution I can think of is to define |
Hi @jaspervdj I just ran into this problem again trying simply check for empty strings in my template. Could it be that a maybeField with the same approach as the haskell Maybe would work? i.e. in |
You can achieve this already in a reasonably clean way. The code I am using is based on the canonical example, i.e. the one generated by <ul>
$for(posts)$
<li>
<a href="$url$">$title$</a> - $date$
</li>
$endfor$
</ul> Let's change that to: $if(posts)$
<ul>
$for(posts)$
<li>
<a href="$url$">$title$</a> - $date$
</li>
$endfor$
</ul>
$else$
<p>There are no posts.</p>
$endif$ Now, an let indexCtx =
listField "posts" postCtx (return posts) `mappend`
constField "title" "Home" `mappend`
defaultContext The code spitting out the list is simply let indexCtx =
listField "posts" postCtx
(if null posts then fail "No posts" else return posts)
`mappend`
constField "title" "Home" `mappend`
defaultContext Does this work for your use case? |
Hi Jasper, Thanks for the answer. I was aware that
Another way might be to allow expressions in the If that's of any use, here's an example of what I'm trying to do which doesn't work because Thanks a lot for your patience (and let me know if it's better to move this into a proper issue rather than stay on this half-baked pull request)! Jun |
This sort of behaviour should work -- if This does not work for |
…around a ()$ block.
This is my first ever Haskell pull request, so apologies for the lack of elegance. This PR is meant to allow using$if()$ blocks surrounding a $for()$ block in templates. It returns empty when a Listfield is empty. This helps for the following (quite common) pattern: