车把中的全局变量(如果有) [英] Global variables in Handlebars if blocks

查看:91
本文介绍了车把中的全局变量(如果有)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在Handlebars条件句中使用全局变量?我正在编写一个列出很多对象的应用程序,并且希望用户能够控制列出哪些详细信息.例如,仅显示人员列表中的名字,如下所示:

Is it possibly to use global variables in Handlebars conditionals? I'm writing an app that lists a lot of objects, and I want users to be able to control which details are listed. For example, displaying only first names in a list of people, like so:

<ul>
{{#each people}}
    <li>
        <p>{{firstName}}</p>
        {{#if displayLastnames}}
        <p>{{lastName}}</p>
        {{/if}}
    </li>
{{/each}}
</ul>

我不想实际修改数据(例如,通过删除lastName属性并执行{{#if lastName}}).

I don't want to actually modify the data (for example, by removing the lastName attribute and doing {{#if lastName}}).

推荐答案

您还可以注册一个名为'displayLastnames'的全局帮助器,并在if中使用它:

You can also register a global helper named 'displayLastnames' and use it in a if :

  Handlebars.registerHelper('displayLastnames', function(block) {
    return displayLastnames; //just return global variable value
  });

并按照您的示例使用它:

and just use it as in your sample :

  {{#if displayLastnames}}
    <p>{{lastName}}</p>
  {{/if}}

这篇关于车把中的全局变量(如果有)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆