Some helpers do not support {:else} as documented
claviska opened this issue ยท comments
As discovered in #104, some helpers do not support the {:else}
block as documented. This is a relatively easy fix for anyone looking to contribute. ๐
The {:else}
block is an optional block in some helpers that will render when a helper fails to find any suitable results. For example:
{@getPosts}
(Display posts here)
{:else}
No posts found
{/getPosts}
In code, the correct way to implement {:else}
looks something like this:
if (!results.length) {
// No results, do {:else} if the block exists
if (bodies['else']) {
chunk = chunk.render(bodies['else'], context);
}
return chunk;
}
Theme Helpers
The following helpers are documented to support the {:else}
block but currently do not.
I'm not sure if this is really a bug. I've tested it with the {@getTags} and the {@getAuthors} Helper and both worked as expected. I've put this in my blog.dust:
{@getTags slug="not-existing-tag"}
{#tags}
{name}
{:else}
No tags
{/tags}
{/getTags}
and got the output: No Tags
in the rendered page.
Maybe this issue is a result of ending up in the catch-block as described here?
I'll have to investigate this further then. I was pretty sure you have to explicitly call the else block. In this case, models.user.findAll
won't throw an error if no results are found โ it will just result in an empty array.
Sorry, it's been awhile since I've worked with Dust.js. Unfortunately, it may have been a bad choice longterm since the lib seems to be deprecated by LinkedIn. In hindsight, Nunjucks may have been a better choice. ๐
I created some dust-Files (blog.dust + post.dust) and implemented all of the above named helpers. They all worked like expected - when used in the right context ;)
So, the {:else}-Block is not working in the following cases:
- the helper function ends up in the .catch-block because of some error in SQL
- the helper is used in the wrong context (eg. {@getRelatedPosts} in {#author}-Context)
I think this is acceptable an this issue can be closed.
Youโre right. I just found this in the Dust wiki:
https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#Logic_in_Templates
Thanks for investigating this!