Mustache is rendered on the server (rails) and on the client (javascript)

Is there any documentation on the best Mustache settings when used 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 is not scalable. I would rather not create my own engine for this.

Is there a more complete template engine that allows you to reuse templates on the server and on the client?

Also, one that takes into account nested patterns on the server and client?

+4
source share
3 answers

There is Poirot : Mustache + Rails 3.

+5
source

not familiar with ruby ​​on rails syntax, but here is my trick:

a) Why do you even want to generate markup on the server side (if this is an option at all), always send json data to the client to the client and allow it to work with it.

b) if you still want to save the server-side rendering engine, then what you can do is save all your mustache templates in a folder, write the script that you execute during the build (or the equivalent in ruby ​​on rails) that combines all templates in a well-chosen JS file with the correct naming conventions.

Something like the following:

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

What do you think about it? Now you have your templates living in one place and you also get the benefits of caching a js file

+2
source

stache gem seams is what you need. Mustache or Rudders + Rails 3 or Rails 4

+1
source

All Articles