Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

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

Closed
6 tasks
claviska opened this issue Sep 30, 2018 · 4 comments
Closed
6 tasks

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

claviska opened this issue Sep 30, 2018 · 4 comments

Comments

@claviska
Copy link
Member

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
Copy link
Contributor

M8inC commented Sep 30, 2018

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
Copy link
Member Author

claviska commented Oct 1, 2018

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 鈥斅爄t 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
Copy link
Contributor

M8inC commented Oct 2, 2018

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.

@claviska
Copy link
Member Author

claviska commented Oct 2, 2018

You鈥檙e right. I just found this in the Dust wiki:

https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#Logic_in_Templates

Thanks for investigating this!

@claviska claviska closed this as completed Oct 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants