Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dust if condition

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?

like image 825
Tom Feeney Avatar asked Feb 22 '13 00:02

Tom Feeney


2 Answers

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).

like image 111
smfoote Avatar answered Oct 08 '22 04:10

smfoote


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
like image 38
cschaefer Avatar answered Oct 08 '22 04:10

cschaefer