1

このようなことをする方法はありますか:

var html,
    templ = '<p>{{config.title}}</p>',
    data = {config: {title: 'some text'}};
html = Mustache.to_html(templ, data);
alert(html);

Mustache ライブラリとは? または、他のライブラリを使用することもできます。

4

3 に答える 3

1

口ひげのコンテキストを使用します。

var html,
    templ = '<p>{{#config}}{{title}}{{/config}}</p>',
    data = {config: {title: 'some text'}};
html = Mustache.to_html(templ, data);
alert(html);

->'<p>some text</p>'

于 2011-04-09T23:03:34.750 に答える
0

口ひげの代わりに underscore.js を使用します。

_.templateSettings = { interpolate : /\{\{(.+?)\}\}/g };
var templateStructure = 'Hello {{ name }}!';
var template = _.template(templateStructure);
alert(template(yourObject));

注意: テンプレートに入れるデータはエスケープされていません。

もう 1 つの考えがあります
。テンプレート構造に「null」が含まれていると、エラーが発生します。それを機能させるには、次のようにします。

var templateStructure = 'Hello {{ name }}! Did You know that "null" will provide an error?';
    templateStructure.replace(/null/g, 'NULL_REPLACEMENT');
var template = _.template(templateStructure);
var out = template(yourObject);
    out = template.replace(/NULL_REPLACEMENT/g, 'null');
alert(out);
于 2011-04-04T08:30:25.900 に答える
0

https://github.com/janl/mustache.js 使い方

mustache.js の使用方法の簡単な例:

var view = {
  title: "Joe",
  calc: function() {
    return 2 + 4;
  }
}

var template = "{{title}} spends {{calc}}";

var html = Mustache.to_html(template, view);

template は口ひげタグを含む単純な文字列で、view はデータとテンプレートをレンダリングするコードを含む JavaScript オブジェクトです。

于 2011-03-28T13:21:57.277 に答える