Skip to content

Instantly share code, notes, and snippets.

@cpinto
Created October 4, 2010 18:21
Show Gist options
  • Save cpinto/610185 to your computer and use it in GitHub Desktop.
Save cpinto/610185 to your computer and use it in GitHub Desktop.
Read the top comments
/*
* Q: What is this?
* A: A sample file that uses mustache to render a single html tmeplate (including partials) on the serverside but keeps those partials intact so that they can be used by the client-side as well (ie. browser)
*
* Q: How is this useful?
* A: To you, I have little idea. I'm doing this because I want to be able to fully render a page on the server and then refresh parts of it on the client, while reusing the widget presentation code. As the widget code is in the file, I just have to bring in mustache.js, fetch new data, and have it render the parts I need to update. And I'd like to keep it all in a single file also.
*
* Q: Isn't that template code broken?
* A: Yes. I'll come back to it as I plan to patch Mu to support xpath partials natively.
* */
var libxml = require('libxmljs');
var sys = require('sys');
var Mu = require('mu');
var xmlstr = '<html><head><title>wonderstache</title></head><body><script type="text/x-mustache-partial" name="widget1"><li>{ {color}}</li></script><h1>Hello {{urname}}</h1><ul id="widgetContainer">{{#colors}}{{>widget1}}{{/colors}}</ul></body></html>';
var sdoc = libxml.parseXmlString(xmlstr);
var partials = {};
sdoc.find('//script[@type="text/x-mustache-partial"]').map(function(tpl){
partials[tpl.attr('name').value()] = tpl.child(0).toString().replace('{ {','{{');
});
res = Mu.compileText(xmlstr,partials);
res({urname:'jan',colors:[{color:'r'},{color:'g'},{color:'b'}]}).addListener('data',function(c){ console.log(c); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment