BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.
http://www.jsviews.com
MIT License
2.68k stars 340 forks source link

Dynamic Template Imports ? #293

Closed daslicht closed 8 years ago

daslicht commented 8 years ago

Hi, is there a way to solve something like this :

{{include tmpl= {{:template}}  /}}

Or is there a template extend ?

Are custom tags meant for this usage ?

daslicht commented 8 years ago

Ok I found a solution myself:

app.get('/', function (req, res) {

        var tmpl = jsrender.templates("./app/home/index.html")
        var html = tmpl.render();
        var context = { hello: "world", template: html};

        res.render('app/layout', context, function(err, html) {
            res.send(err ? jsrender.views.converters.html(err.message) : html); // HTML encode message
        });
});
I am the Layout, next line is an include: <br>
<hr>

<!-- {{include tmpl='./app/home/index.html' /}} <br> -->
{{:hello}}
{{>template}} <br>
<hr>
{{include tmpl='./app/_include/footer.html' /}}

Are there any downsides of this solution ?

daslicht commented 8 years ago

is there a way to include a template with context ?

BorisMoore commented 8 years ago

tmpl=anyExpression - it does not have to be the statically-defined name or selector string...

http://www.jsviews.com/#samples/jsr/composition/tmpl-objects

http://stackoverflow.com/questions/35627356/dynamically-assigning-different-templates-based-on-data-using-helper-or-custom

daslicht commented 8 years ago

Thank you !