I am using node.js on the server side, express.js and jade. I wrote a small wrapper function to populate client-side jade patterns. I think I will use requireJS and jQuery on the client side, but have not decided yet. Now the task that I have to do many times is
- template selection (from server or cache)
- fetching data from the server
- fill in the template and paste it in / instead of the element
Note: there are many templates, and my question is not about the template engine, but about a simple process.
I have to do it like this:
var get_data = function (tpl) {
$.get(url, function(data) {
$('#target_element').html(jade.render(tpl, {locals: data}));
});
};
if (!'template_name' in _cache) {
$.get('template_name', function(tpl) {
_cache['template_name'] = tpl;
get_data(tpl);
});
}
else {
get_data(_cache['template_name']);
}
(in this example, the template and data are selected synchronously, which is not very nice)
I would like to have a code like this:
render_template('template_name', 'url?arguments=values', {replace: '#element_id'});
(it is similar to MongoDB syntax)
Is there a simple jquery structure or module to do this job?