9

I'm having trouble with a dust if condition. I have a partial that points to 2 different dust templates depending on the Country code

{>"receipt/merchantInfo/merchantInfo_{countryCode}"/}

I'm trying to make an if else condition that will figure out if the {countryCode} is US. Example:

{@if cond="'{countryCode}' == 'US'"}
<p>is country code US</p>
{:else}
<p>is NOT country code US</p>
{/if}

This isn't working. Anyone have an idea where I went wrong with this?

nbro
  • 15,395
  • 32
  • 113
  • 196
Tom Feeney
  • 97
  • 1
  • 1
  • 3

4 Answers4

22

The @if helper has been deprecated because of a potential security hole (it uses eval on the condition). I would recommend using the @eq helper instead.

{@eq key=countryCode value="US"}
  <p>is country code US</p>
{:else}
  <p>is NOT country code US</p>
{/eq}

If that doesn't work, make sure countryCode is available in your context (you can use {@contextDump/} for that).

smfoote
  • 5,449
  • 5
  • 32
  • 38
  • Upvoted for mentioning the security hole. How about `select`? Can I use `select` with `eq`, `gte` etc? and how can I write complex conditions? Lets say `A > B && B <= C && C != 10` – thefourtheye Aug 16 '13 at 11:44
  • 1
    `select` can be used with `eq`, `gt`, `lt`, etc. Complex conditionals aren't currently possible in a single statement. You have to use nesting and usually end up repeating some code. There have been talks of create `and` and `or` helpers, but in the meantime, I would recommend changing your data, if possible. – smfoote Aug 16 '13 at 20:52
  • Can you please provide source for this? The code/website for dustjs-helpers does not mention that "if" is deprecated. – Eirik Hoem Aug 18 '14 at 07:41
  • Thanks for mentioning contextDump, that was very handy – Paul Sheldrake Apr 06 '16 at 23:15
10

I would do this:

{@select key=countryCode }
    {@eq value="US"}<p>is country code US</p>{/eq}
    {@eq value="UK"}<p>is country code UK</p>{/eq}
    {@default}<p>is NOT country code US or UK</p>{/default}
{/select}

Make sure you are using version 1.x or later.

Make sure you have the Helpers loaded. I ran into this the other day. You need it for this to work and it won't error telling you that it isn't.

Jeremy
  • 285
  • 3
  • 14
  • fyi. "default" is deprecated in the latest version of dust https://stackoverflow.com/questions/15014853/dust-if-condition – RayLoveless Dec 04 '18 at 23:54
2

If you are enamored of @if but don't like the security issues around its use of eval, you can use my alternative @if helper. It provides an attribute test="expr" to specify your if condition. eval is NOT used to evaluate the expression.

Variables in the expression are restricted to dust names and path used to access values from the context. Constants are JavaScript integer, float, hex and string forms ("xx" or 'xx'). Operands can be a "variable", a constant, or a binary or unary expression yielding a value. Relational operators are <, >, <=, >=, ==, !=. Boolean operators are ! (unary), || and &&.. Operator precedence is the same as JavaScript and parentheses are allowed for clarity or for when the precedence is not what you want.

Here is an example:

{@if test="state == 'CA' || state == 'NY'"}
    true stuff goes here
{:else}
    false stuff goes here
{/if}

Note that it still has code to allow the cond="expr" attribute that uses eval(). This provides a migration path for existing code.

You can install it as an npm module (https://npmjs.org/package/dustmotes-if).

rragan
  • 484
  • 2
  • 2
2

You can also use:

{#key}
  Some content
{:else}
  Some other content, if key doesn't have a value
{/key}

For example:

Data

{
   firstName: "Mickey",
   lastName:  "Mouse",
   website:   null,
   phone:     "1-800-DISNEYWORLD"
}

Dust Template

<p>First Name: {firstName}</p>
<p>Last Name: {lastName}</p>
{#website}
   <p>Website: {website}</p>
{:else}
   <p>Phone: {phone}</p>
{/website}

Output:

First Name: Mickey
Last Name: Mouse
Phone: 1-800-DISNEYWORLD
cschaefer
  • 259
  • 4
  • 8