dustjs icon indicating copy to clipboard operation
dustjs copied to clipboard

Context helpers does not work with conditionals

Open lkmill opened this issue 10 years ago • 3 comments

The dustjs manual at http://www.dustjs.com/guides/context-helpers/#writing-context-helpers insinuates that context helpers can be used with both sections ({#helper}) and conditionals ({?helper}). However, conditionals only seem to test truthy/falsy... thus always returning true for functions/helpers.

the following code always console.logs <h1></h1><h1>? PASS</h1>.

var data = {
  helper: function() {
    return false;
  }
}

var src = '<h1>{#helper}# PASS!{/helper}</h1><h1>{?helper}? PASS{/helper}</h1>';

dust.renderSource(src, data, function(err, html) {
  console.log(html);
})

lkmill avatar Aug 13 '15 12:08 lkmill

Could you elaborate on which part of the docs insinuate that?

? and ^ are for "exists / not exists"-- they only check the existence of a helper.

If you return true and false from a helper, they act like conditionals, however. So something like

{#check}This sentence is false!{/check}

Will render if check returns true, and not render at all if it returns false.

sethkinast avatar Aug 13 '15 16:08 sethkinast

Well, in the section i linked to in my post. To be more specific...

In the "data" code this helper is defined:

"hasFriendsHelper": function() {
    return this.friends.length > 0;
  }

and in the "template" code it is referred to as thus:

{?hasFriendsHelper}Yay friends!{/hasFriendsHelper}

lkmill avatar Aug 16 '15 17:08 lkmill

Perfect, I missed that the first time. Will get it changed-- thank you!

On Sun, Aug 16, 2015 at 10:17 AM, Linus Miller [email protected] wrote:

Well, in the section i linked to in my post. To be more specific...

In the "data" code this helper is defined:

"hasFriendsHelper": function() { return this.friends.length > 0; }

and in the "template" code it is referred to as thus:

{?hasFriendsHelper}Yay friends!{/hasFriendsHelper}

— Reply to this email directly or view it on GitHub https://github.com/linkedin/dustjs/issues/693#issuecomment-131584181.

Seth Kinast http://sethkinast.com/

sethkinast avatar Aug 17 '15 02:08 sethkinast