Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js and swig template engine - including template inside template

Node.js and swig template engine - including template inside template

Problem

I trying to create main page (part of node.js and mongoDB application) that includes login form. To add view part I included js files with function that returns HTML, but as I can see much better is using template engine. Everything is OK until I including one compiled swig part inside another one. The output of main page is OK, but login part outputs like text on the page. How is possible to output the login HTML as HTML instead of plain text?

Does more information needed to understand the issue?

Thank you in advance.

var swig  = require('swig');
var mainPage_tpl = swig.compileFile(__dirname+'/../views/mainpage_tpl.html');
var formLogin_tpl = swig.compileFile(__dirname+'/../views/login_tpl.html');

var loginOutput =  formLogin_tpl();
var mainPageOutput = mainPage_tpl({
    title: 'Sometitle',
    pagetitle: 'Somepagetitle',
    content: loginOutput
});

exports.get = function(req, res){
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(mainPageOutput);
    res.end();
}

mainpage_tpl.html:




    {{title}}

{{pagetitle}}

{{content}}
Problem courtesy of: Pumych

Solution

If you want to include literal HTML, you need to tell Swig to not escape it using the safe filter:

...
{{ content|safe }}
...
Solution courtesy of: robertklep

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js and swig template engine - including template inside template

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×