Meteor handlebars template how to eval parameters

依然范特西╮ 提交于 2019-12-13 07:56:17

问题


I have a participant being rendered in this template:

<template name="participant">
    <div class="animated fadeIn">
      <a href="{{pathFor 'participants'}}">← Back</a>
      {{#with participant}}
        <div class="participant">
          <h3>{{fullname}}</h3>
            <dl>
                <dt>E-mail</dt>
                <dd>{{email}}</dd>
                <dt>Phone</dt>
                <dd>{{tel}}</dd>
                <dt>City</dt>
                <dd>{{zip}} {{city}}</dd>
                <dt>Creation time</dt>
                <dd>added {{created_on}}</dd>

            </dl>
            {{>quickfield name="email" value=email}}
          <p>&nbsp;</p>
          <h5><a href="#" class="delete">Delete</a></h5>
        </div>
      {{/with}}
    </div>
</template>

Where Quickfield template is:

<template name="quickfield">
    <input id="{{name}}" value="{{value}}" class="bound">
</template>

I would like to skip the second parameter, called "value", in the template call, as it is the same as the name parameters. We should be able to build the template with only one parameter.

Ideally, I should have this:

{{>quickfield name="email"}}
<template name="quickfield">
    <input id="{{name}}" value="{{eval('this.' + name)}}" class="bound">
</template>

But that doesn't work. How can I do?

Thks!


回答1:


Create an additional helper:

UI.registerHelper('get', function(name) {
  return this[name];
});

Now you can rewrite your template as:

<template name="quickfield">
  <input id="{{name}}" value="{{get name}}" class="bound">
</template>


来源:https://stackoverflow.com/questions/24199949/meteor-handlebars-template-how-to-eval-parameters

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