このようなことをする方法はありますか:
var html,
templ = '<p>{{config.title}}</p>',
data = {config: {title: 'some text'}};
html = Mustache.to_html(templ, data);
alert(html);
Mustache ライブラリとは? または、他のライブラリを使用することもできます。
このようなことをする方法はありますか:
var html,
templ = '<p>{{config.title}}</p>',
data = {config: {title: 'some text'}};
html = Mustache.to_html(templ, data);
alert(html);
Mustache ライブラリとは? または、他のライブラリを使用することもできます。
口ひげのコンテキストを使用します。
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>'
口ひげの代わりに 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);
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 オブジェクトです。