trolkura trolkura - 1 year ago 91
Jade Question

Dynamicly creating elements inside pug(jade)

I am trying to dynamicly create elements inside pug template.

I retrieve an informations from database using basic command

res.render('stock' ,{ table:component})

the template that the content is supposed to be created is

table(cellpadding='0', cellspacing='0', border='0')
- row in table
td= "#{}"
td= "#{}"

I have checked the syntax and it should be correct , but the result is complete wrong. It takes
row in table
as string e.g content and show it on page , same with

Am i using some outdated syntax or is my approach completely wrong?


Answer Source

Your used syntax seems to be wrong if you're using the default pug template engine for Node.js

You can make it work by:

    table(cellpadding='0', cellspacing='0', border='0')
       each row in table

Explaned in more detail in the documentation:

My complete simple Node.js test file with which I did the successfull testing was:

var express = require('express');
var app = express();
app.set('view engine', 'pug');

var component = [
    {name:'myName', storage:'myStorage'},
    {name:'myName2', storage:'myStorage2'}
app.get('/table', function (req, res) {
    res.render('stock' ,{ table:component});


It results in the response:

<div class="tbl-content">
 <table cellpadding="0" cellspacing="0" border="0">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download