简体   繁体   English

Handlebars.js - 构建模板模板

[英]Handlebars.js - Building a Template Template

TL;DR TL; DR

In the following Handlebars template... 在以下Handlebars模板中......

<div class="field field-label">
  <label>{{label}}</label><input type="text" value="{{{{attribute}}}}">
</div>

I need {{attribute}} to be evaluated but value="{{ value of attribute }}" to be printed. 我需要评估{{attribute}},但要打印value="{{ value of attribute }}"

Background 背景

I've got an interesting use for templating. 我有一个有趣的模板用途。 My application has dozens of forms (and growing!), and several ways to display them. 我的应用程序有几十种形式(并且还在增长!),以及几种显示它们的方法。 Obviously they can be displayed in the browser, or a mobile device, or a PDF, etc... So what I'd like to do is define these forms in JSON to live somewhere like MongoDB. 显然,它们可以显示在浏览器,移动设备或PDF等中......所以我想做的是在JSON中定义这些表单,以便像MongoDB一样生活。 That way, they can be easily modified without the HTML views, mobile apps, and PDF render function all being updated. 这样,可以轻松修改它们,而无需更新HTML视图,移动应用程序和PDF呈现功能。

{
  title: 'Name of this Form',
  version: 2,
  sections: [
    { 
      title: 'Section Title',
      fields: [
        {
          label: 'Person',
          name: 'firstname',
          attribute: 'FIRST',
          type: 'text'
        }, {
          label: 'Birthday',
          name: 'dob',
          attribute: 'birthday',
          type: 'select',
          options: [
            { label: 'August' },
            { label: 'September' },
            { label: 'October' }
          ]
        },
        ...
        ...

That's a taste. 这是一种品味。 So type: 'text' results in <input type="text"> , name is the name of the input, attribute is the attribute from the model, yada yada. 所以type: 'text'结果为<input type="text">name是输入的名称, attribute是来自模型的属性yada yada。 It gets fairly complex with nested optional forms, but you get the point. 嵌套的可选表单相当复杂,但你明白了。

The problem is, now I've got two contexts . 问题是,现在我有两个背景 The first being the JSON with the form data, and the second being the JSON from the model. 第一个是带有表单数据的JSON,第二个是来自模型的JSON。 I've got two options I think would work well. 我认为有两种选择可行。

Solution 1 解决方案1

Quick little closure containing the model context registered as a helper. 包含模型上下文的快速小闭包注册为帮助程序。

var fn = (function(model) {
  return function(attr) {
    return model[attr]
  }
})(model);

Handlebars.registerHelper('model', fn)

...to be used like so... ......像这样使用......

<input type="text" name="{{name}}" value="{{model attribute}}">

Solution 2 解决方案2

Two passes. 两次通过。 Have my template output a template that I can then compile and run with my model. 让我的模板输出一个模板,然后我可以编译并运行我的模型。 One big advantage, I can precompile the forms. 一个很大的优点,我可以预编译表单。 I would prefer this approach. 我更喜欢这种方法。 Here's my question. 这是我的问题。 How do I print out {{attribute}} from my templates? 如何从模板中打印出{{attribute}}?

For example, in my text template... 例如,在我的文本模板中......

<div class="field field-label">
  <label>{{label}}</label><input type="text" value="{{{{attribute}}}}">
</div>

I need {{attribute}} to be evaluated and {{ value of attribute }} to be printed. 我需要{{attribute}}将被打印到进行评估和{ 属性的值 {}}。

I went with solution 2, kinda. 我选择了解决方案2,有点儿。 It was important to me that I could precompile the forms sans data. 对我来说,重要的是我可以预先编译表单sans数据。 So what I did was just add a few helper functions... 所以我所做的只是添加一些辅助函数......

Handlebars.registerHelper('FormAttribute', function(attribute) { 
  return new Handlebars.SafeString('{{'+attribute+'}}');    
});

Handlebars.registerHelper('FormChecked', function(attribute) {
  return new Handlebars.SafeString('{{#if ' + attribute + '}}checked="checked"{{/if}}');
});

...that I could use in my form templates... ...我可以在我的表单模板中使用...

<input type="text" name="{{name}}" value="{{FormAttribute attribute}}">

...that results in... ......结果......

<input type="text" name="FirstName" value="{{FirstName}}">

I would still be interested to learn if there's some way to have Handlebars ignore and not parse curly brackets {{}} without using helpers. 我仍然有兴趣了解是否有一些方法可以忽略Handlebars而不使用帮助器解析大括号{{}}。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM