admin管理员组文章数量:1394769
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 plete 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?
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 plete 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?
Share Improve this question edited Aug 12, 2011 at 21:01 Mark Peterson asked Aug 12, 2011 at 19:59 Mark PetersonMark Peterson 5705 silver badges20 bronze badges 1- I have found that the best way to acplish this is by using Node.js with Express webserver, RequireJS, Backbone.js, and any template engine. Works beautifully on the client or the server. – Mark Peterson Commented Oct 10, 2013 at 18:25
3 Answers
Reset to default 5There is Poirot available: Mustache + Rails 3.
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 bines all the templates into a nicely scoped JS file with the right naming conventions.
Something as follows:
var MUSTACHE_TEMPLATES= MUSTACHE_TEMPLATES || (function(){
var template1= "<big ass template>";
var template2="<small template>";
return
{
template1: template1,
template2: template2
}
}());
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
The stache gem seams to be what you need. Mustache or handlebars + Rails 3 or Rails 4
本文标签: Mustache render on the server (rails) and on the client (javascript)Stack Overflow
版权声明:本文标题:Mustache render on the server (rails) and on the client (javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744100729a2590856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论