this is my html:
<script type="text/html" id="ul-template"> <ul id="list"> {{> li-templ}} </ul> </script> <script type="text/html" id="ul-template2"> <div id="list2"> {{> li-templ}} </div> </script> <script type="text/html" id="li-templ"> <p>{{ name }}</p> </script>
as you can see, I want to reuse the #li-templ
part, but it seems that I have to write it into a file called li-templ.mustache
then I can include it as partial
?
can I just define them in the single html file?
render = function (template, view, partials) { return this. compile(template)(view, partials); }; This is the most basic form of templating with mustache. Let's see the other methods available for creating more organized code.
The object should be keyed by the name of the partial, and its value should be the partial text. You'll have to retrieve the template's text and provide that to Mustache.render() (e.g., with jQuery): $. get('path/template.mustache', function (partialTemplate) { var html = Mustache.
Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object. We call it "logic-less" because there are no if statements, else clauses, or for loops.
I'm assuming you're using the JS flavor of Mustache.
In mustache.js an object of partials may be passed as the third argument to Mustache.render. The object should be keyed by the name of the partial, and its value should be the partial text.
You need to:
Here's some jQuery to do just that:
var view = {"name" : "You"}, li = $('#li-templ').html(), partials = {"li-templ": li}, ul1 = Mustache.to_html($('#ul-template').html(), view, partials), ul2 = Mustache.to_html($('#ul-template2').html(), view, partials);; document.write(ul1, ul2);
Here's a jsFiddle of it all working- http://jsfiddle.net/maxbeatty/EYDfP/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With