javascript - 即时评估模板

标签 javascript meteor

--使用 Meteor 框架--

您好,我想通过 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))

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

最佳答案

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

相关文章:

Javascript函数作为另一个函数的参数?

javascript - 为什么我会因为这个 Javascript 函数而失去利润

javascript - Meteor 路由器在开发模式下工作,但在生产模式下不工作

javascript - 在iron-router中使用查询参数修改模板

javascript - 将图像从另一个网站拖放到我的

javascript - 如何将 jquery ui 对话框定位在具有多个 iframe 的屏幕中心..?

javascript - 在 javascript 的 google maps api 中显示楼层选择器

jquery - meteor : how to add multiple js and css file based on templates. ?

Meteor 0.5.0 示例未在 Windows 上运行

node.js - 使用 Node 的 Mongodb find() 不返回所有文档(奇怪的行为)