Mustache 在服务器 (rails) 和客户端 (javascript) 上渲染
是否有关于在服务器(使用 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!") %>
以上是不可扩展的。我不想为此制作自己的引擎。
是否有更完整的模板引擎允许在服务器和客户端上重用模板?
此外,是否有一个可以解释服务器和客户端上的嵌套模板的问题?
Is there any documentation on Mustache best practices when using on the server (with rails) and on the client (with javascript)?
# 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!") %>
The above isn't scalable. I'd prefer not to make my own engine for this.
Is there a more complete templating engine that allows reuse of templates on the server and on the client?
Additionally, one that accounts for nested templates on the server and the client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有 Poirot 可用:Mustache + Rails 3。
There is Poirot available: Mustache + Rails 3.
不熟悉 ruby on Rails 语法,但这是我的看法:
a)为什么要在服务器端生成标记(如果有的话)总是将 json 数据发送到客户端并让 js Mustache 引擎处理它
b) 如果您仍然想保留服务器端渲染引擎,那么您可以做的是将所有 Mustache 模板保存在一个文件夹中,编写一个在构建期间执行的脚本(或 ruby on Rails 中的等效脚本)将所有模板组合成一个具有正确命名约定的良好范围的 JS 文件。
内容如下:
您对这种方法有何看法?现在,您的模板位于单个位置,并且您还可以获得 js 文件被缓存的优势
not familiar with the ruby on rails syntax but here is my take:
a) Why do you want to generate the markup on the server side at all (if at all that is an option) always send in json data to the client and let js mustache engine deal with it
b) if you still want to keep your server side rendering engine, then what you can do is keep all your mustache templates in a folder write a script that you execute during your build (or equivalent in ruby on rails) that combines all the templates into a nicely scoped JS file with the right naming conventions.
Something as follows:
What do you think of that approach? Now you have your templates living in a single location and you also get the advantages of the js file being cached
stache gem 接缝就是您所需要的。小胡子或车把 + Rails 3 或 Rails 4
The stache gem seams to be what you need. Mustache or handlebars + Rails 3 or Rails 4