4

在服务器(使用 rails)和客户端(使用 javascript)上使用时,是否有关于 Mustache 最佳实践的文档?

# hello_world.mustache
Hello {{planet}}

# some other file
<%
hello_world_template = File.read(File.dirname(__FILE__) + "/hello_world.mustache")
%>

<script id="hello_world_template" type="text/x-jquery-tmpl"> 
    <%= hello_world_template %>
</script>

<script>
    // $.mustache = using mustache.js and a jquery mustache wrapper 
    // search on "Shameless port of a shameless port"
    document.write($.mustache($("#hello_world_template").html(), { "planet" : "World!" }));
</script>

<%= Mustache.render(hello_world_template, :planet => "World!") %>

以上是不可扩展的。我不想为此制作自己的引擎。

是否有更完整的模板引擎允许在服务器和客户端重用模板?

此外,一个解释服务器和客户端上的嵌套模板?

4

3 回答 3

5

Poirot可用:Mustache + Rails 3。

于 2011-08-12T20:17:42.763 回答
2

不熟悉 ruby​​ on rails 语法,但这是我的看法:

a)为什么要在服务器端生成标记(如果有的话)总是将json数据发送到客户端并让js mustache引擎处理它

b)如果您仍然想保留服务器端渲染引擎,那么您可以做的是将所有胡子模板保存在一个文件夹中编写一个您在构建期间执行的脚本(或在 ruby​​ on rails 中执行的等效),它结合了所有模板使用正确的命名约定放入一个范围很好的 JS 文件中。

如下所示:

var MUSTACHE_TEMPLATES= MUSTACHE_TEMPLATES || (function(){
var template1= "<big ass template>";
var template2="<small template>";

return
{
template1: template1,
template2: template2
}
}());

您如何看待这种方法?现在您的模板位于一个位置,并且您还获得了缓存 js 文件的优势

于 2011-08-12T20:18:57.083 回答
1

stache宝石接缝是您所需要的小胡子或车把 + Rails 3 或 Rails 4

于 2015-03-05T09:21:25.377 回答