即时评估模板

时间:2013-11-19 06:32:49

标签: javascript meteor

- 使用METEOR FRAMEWORK -

您好我想通过Template["tViewPost"]

访问以下模板
<template name="tViewPost">
    <div class="breadcrumb">
        <span>{{title}}</span>&lsaquo;&lsaquo;<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))

有可能做到吗?如何?谢谢...

1 个答案:

答案 0 :(得分:2)

您可以尝试使用 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)))