Dust if condition

烈酒焚心 提交于 2020-01-01 08:46:29

问题


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?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/15014853/dust-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!