Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 打印object.object并留胡子_Javascript_Mustache - Fatal编程技术网

Javascript 打印object.object并留胡子

Javascript 打印object.object并留胡子,javascript,mustache,Javascript,Mustache,有没有办法做到这一点: var html, templ = '<p>{{config.title}}</p>', data = {config: {title: 'some text'}}; html = Mustache.to_html(templ, data); alert(html); var html, templ='{{config.title}}', 数据={config:{title:'some text'}}; html=Mustache

有没有办法做到这一点:

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

', 数据={config:{title:'some text'}}; html=Mustache.to_html(模板,数据); 警报(html);
用这个? 或者是其他图书馆。

用法

如何使用mustache.js的一个简单示例:

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

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

var html = Mustache.to_html(template, view);
template是一个带有mustache标记的简单字符串,view是一个JavaScript对象,包含用于呈现模板的数据和任何代码。

用法

如何使用mustache.js的一个简单示例:

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

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

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

template是一个带有mustache标记的简单字符串,view是一个JavaScript对象,包含数据和用于呈现模板的任何代码。

使用下划线.js代替mustache:

_.templateSettings = { interpolate : /\{\{(.+?)\}\}/g };
var templateStructure = 'Hello {{ name }}!';
var template = _.template(templateStructure);
alert(template(yourObject));
小心:您放在模板中的数据是不可替换的

还有一个想法:
如果templateStructure包含“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);

使用下划线.js代替胡须:

_.templateSettings = { interpolate : /\{\{(.+?)\}\}/g };
var templateStructure = 'Hello {{ name }}!';
var template = _.template(templateStructure);
alert(template(yourObject));
小心:您放在模板中的数据是不可替换的

还有一个想法:
如果templateStructure包含“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);

使用胡须上下文:

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

',, 数据={config:{title:'some text'}}; html=Mustache.to_html(模板,数据); 警报(html);

->
一些文本

使用胡须上下文:

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

',, 数据={config:{title:'some text'}}; html=Mustache.to_html(模板,数据); 警报(html);
->
“一些文本