【问题标题】:How to parse object key from handlebars partials.如何从车把部分解析对象键。
【发布时间】:2017-12-30 17:52:00
【问题描述】:

express 中使用express-handlebars 作为视图引擎和handlebars helpers。我为选择标签创建了小部分,但它将普通键作为字符串。

我的选择部分 select.hbs 文件

<select name="" id="">
  <option value="">Select</option>
  {{#forEach this.select_values}}
    <option value="{{../this.opt_value}}"> {{ ../this.opt_label }} </option>
  {{/forEach}}
</select>

帐户中传递对象数组

accounts: [
      {
        name: 'John',
        email: 'john@example.com'
      },
      {
        name: 'Malcolm',
        email: 'malcolm@example.com'
      },
      {
        name: 'David',
        email: 'david@example.com'
      }
    ]

在布局中调用部分

{{> modules/select select_values=accounts opt_value='name' opt_label='email'}}

我正在为

使用波纹管依赖项
"dependencies": {
    "body-parser": "~1.18.2",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "express": "~4.15.5",
    "express-handlebars": "^3.0.0",
    "handlebars-helpers": "^0.10.0",
    "hbs": "^4.0.1",
    "morgan": "~1.9.0",
    "serve-favicon": "~2.4.5"
  }

【问题讨论】:

    标签: express handlebars.js handlebarshelper express-handlebars


    【解决方案1】:

    它适用于车把builtin helper lookup 从车把添加默认的{{#each}} 循环。

    <select name="" id="">
      <option value="">Select</option>
      {{#each this.select_values}}
        <option value="{{lookup this ../this.opt_value}}"> {{lookup this  ../this.opt_label }} </option>
      {{/each}}
    </select>
    

    特别感谢@Tamlyn 对查找助手的解释。

    【讨论】:

    • 您的示例中的代码使用查找助手。
    • 我粘贴错误的旧代码将使其正确谢谢
    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 2016-07-21
    • 2016-03-02
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多