--使用 Meteor 框架--
您好,我想通过 Template["tViewPost"]
访问以下模板
<template name="tViewPost">
<div class="breadcrumb">
<span>{{title}}</span>‹‹<span>{{subttile}}</span>
</div>
</template>
并且能够使用像{ title : "My title", subttitle : "othe subtitle"};
这样的javascript对象来渲染/评估这个模板。但一旦我将模板放入变量中,我不知道如何执行此操作,我想像下划线库那样执行此操作。[http://underscorejs.org/]
中的示例]
var template = _.template("whatever <%= title %>");
var o = {title : "ohhh!"};
$("someDomElement").html(template(o))
有可能做到吗?如何?谢谢...
最佳答案
您可以尝试使用 Meteor.render 执行此操作(来自文档):
// Client side: show the number of players online.
var frag = Meteor.render(function () {
return "<p>There are " + Players.find({online: true}).count() +
" players online.</p>";
});
document.body.appendChild(frag);
// Server side: find all players that have been idle for a while,
// and mark them as offline. The count on the screen will
// automatically update on all clients.
Players.update({idleTime: {$gt: 30}}, {$set: {online: false}});
编辑:
// returns string which contains html
Meteor.render(Template['name'](dataObject))
// your case:
<template name="test">
whatever {{title}}"
</template>
var o = {title : "ohhh!"};
$("someDomElement").html(Meteor.render(Template['test'](o)))
关于javascript - 即时评估模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064748/