Dust if condition
Asked Answered
O

4

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?

Otho answered 22/2, 2013 at 0:30 Comment(1)
*condition just to make it easier to fint itLiber
A
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).

Anson answered 22/2, 2013 at 2:0 Comment(4)
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 != 10Grogan
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.Anson
Can you please provide source for this? The code/website for dustjs-helpers does not mention that "if" is deprecated.Dagley
Thanks for mentioning contextDump, that was very handyOke
B
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.

Boethius answered 9/5, 2013 at 20:54 Comment(1)
fyi. "default" is deprecated in the latest version of dust https://mcmap.net/q/1118458/-dust-if-conditionMallon
H
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).

Hoxsie answered 5/2, 2014 at 17:36 Comment(0)
S
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
Shana answered 6/1, 2017 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.