1

I am looking to get the following logic in Handlebars.js

  • the following line should appear in the output if foo is truthy

    xxx.yyy.zzz = {{foo}}

  • no line should appear if foo is falsy

How can I do it?

UPDATE:

  • I know I can wrap the line into an #if condition, but it's too wordy, is there a more elegant way?

UPDATE:

  • The question is rather:
    • Whether or not Handlebars can handle it without an #if wrap for a single line?
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
  • https://stackoverflow.com/q/34252817/3397771 – 76484 Sep 30 '20 at 17:43
  • this is cumbersome – Trident D'Gao Sep 30 '20 at 17:43
  • Cumbersome? If you want to render something conditionally then you will need to include the condition. I'm not sure what uncumbersome solution you had in mind. – 76484 Sep 30 '20 at 17:47
  • truthy is the keyword, something like this: `xxx.yyy.zzz = {{if foo}}` – Trident D'Gao Sep 30 '20 at 17:48
  • that said, i am looking for sugar – Trident D'Gao Sep 30 '20 at 17:51
  • 1
    *Truthy* is precisely what [an `#if` helper](https://handlebarsjs.com/guide/builtin-helpers.html#if) covers. If the goal is to control the appearance of `xxx.yyy.zzz = ` as well as `{{foo}}`, the condition has to wrap the entire line. – Jonathan Lonowski Sep 30 '20 at 17:56
  • yes, the status-quo requires a wrapper, i am looking to get past it for a single line, can handlebars do it? – Trident D'Gao Sep 30 '20 at 17:57
  • 1
    If you're able to find this sugar, I don't think you'll find it very sweet. – 76484 Sep 30 '20 at 18:03
  • Not precisely as you envisioned. A helper can't effect text outside of its braces. Though, a custom helper could probably be defined with a [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) to handle `{{put-if foo 'xxx.yyy.zzz = ' foo}}`. If you don't like repeating `foo`, perhaps give it placeholders: `{{put-if foo 'xxx.yyy.zzz = ?'}}`. – Jonathan Lonowski Sep 30 '20 at 18:13

0 Answers0