Handlebars 在 helper 处获取对象值,无需使用数据进行编译
Handlebars getting object value at helper, without compile with data
<h1>Handlebars JS Example</h1>
<script id="some-template" type="text/x-handlebars-template"> <table>
{{helper yes}}
</script>
var source = $("#some-template").html();
var template = Handlebars.compile(source);
var data = {
yes:'Yes'
};
Handlebars.registerHelper('helper', function(person) {
console.log(person);
console.log(data);
console.log(data[person]);
console.log(myFunction(person));
});
$('body').append(template(data));
function myFunction(keyname) {
return data[keyname];
};
我尝试通过从车把获取的字符串获取数据值。
Why no methods that i tried works?
现在我真的找到了解决方案,问题是当编译模板时我们可以像我的示例一样将对象放入它。但我需要从 var 中获取它并按名称查找它。所以当我使用 up 之类的参数时,它仍然说找不到 属性 是的。我发现 here that as argument we can use string. And its work, my code will be cleaner. jsFiddle
var source = $("#some-template").html();
var template = Handlebars.compile(source);
var data = {
yes:'Yes'
};
<h1>Handlebars JS Example</h1>
<script id="some-template" type="text/x-handlebars-template">
{{helper 'yes'}}
</script>
Handlebars.registerHelper('helper', function(person) {
return data[person];
});
$('body').append(template());
<h1>Handlebars JS Example</h1>
<script id="some-template" type="text/x-handlebars-template"> <table>
{{helper yes}}
</script>
var source = $("#some-template").html();
var template = Handlebars.compile(source);
var data = {
yes:'Yes'
};
Handlebars.registerHelper('helper', function(person) {
console.log(person);
console.log(data);
console.log(data[person]);
console.log(myFunction(person));
});
$('body').append(template(data));
function myFunction(keyname) {
return data[keyname];
};
我尝试通过从车把获取的字符串获取数据值。 Why no methods that i tried works?
现在我真的找到了解决方案,问题是当编译模板时我们可以像我的示例一样将对象放入它。但我需要从 var 中获取它并按名称查找它。所以当我使用 up 之类的参数时,它仍然说找不到 属性 是的。我发现 here that as argument we can use string. And its work, my code will be cleaner. jsFiddle
var source = $("#some-template").html();
var template = Handlebars.compile(source);
var data = {
yes:'Yes'
};
<h1>Handlebars JS Example</h1>
<script id="some-template" type="text/x-handlebars-template">
{{helper 'yes'}}
</script>
Handlebars.registerHelper('helper', function(person) {
return data[person];
});
$('body').append(template());