最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Mustache render on the server (rails) and on the client (javascript) - Stack Overflow

programmeradmin8浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

There 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

发布评论

评论列表(0)

  1. 暂无评论