Postleaf / postleaf

Simple, beautiful publishing with Node.js.
https://www.postleaf.org/
MIT License
505 stars 204 forks source link

Some helpers do not support {:else} as documented #105

Closed claviska closed 5 years ago

claviska commented 5 years ago

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.

M8inC commented 5 years ago

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?

claviska commented 5 years ago

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. ๐Ÿ˜•

M8inC commented 5 years ago

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:

I think this is acceptable an this issue can be closed.

claviska commented 5 years ago

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!