把手如果var长度大于

时间:2019-05-29 08:44:35

标签: javascript handlebars.js zurb-foundation

我目前正在尝试使用Foundation email和Handlebars设置一些电子邮件模板。我试图根据是否将值传递给组件来呈现一些标题,但是我似乎无法使其正常工作。我在这里做什么错了?

heading-1.html

<h1>{{text}}</h1>

paragraph.html

<row class="card">
    <columns>
        <wrapper class="inner-padding">
            {{#if (eq heading-1.length > 0)}}
                {{> heading-1 text="Test"}}
            {{else}}
                <h1>failed</h1>
            {{/if}}
            <p>
                {{text}}
            </p>
        </wrapper>
    </columns>
</row>

index.html

<container>
  <row>
    <columns class="content">
      {{> paragraph
        heading-1="test"
        text="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet dolorem molestias nam odit suscipit velit.
        Ab aliquam at autem dignissimos dolorum ipsam magnam molestiae officia, quae quo, veniam veritatis voluptatibus?"
      }}
    </columns>
  </row>
</container>

1 个答案:

答案 0 :(得分:1)

创建车把助手:

Handlebars.registerHelper('checklength', function (v1, v2, options) {
'use strict';
   if (v1.length>v2) {
     return options.fn(this);
  }
  return options.inverse(this);
});

使用:

{{#checklength Title 0}}   //Title is property and 0 is length
             <p>Heading</p>
      {{else}}
             <h1>failed</h1>
      {{/checklength}}