简体   繁体   English

将本地javascript变量的值替换为Pug的插值

[英]Substitute local javascript variable's value into Pug's interpolation

I am new to pug and now I am trying to render a html view which contains a table whose entries are parsed from app.js 我是pug的新手,现在我试图呈现一个html视图,其中包含一个表,该表的条目是从app.js中解析的

this is my index.pug 这是我的index.pug

doctype html
html(lang='en')
    head
        title Pug
    body
        table
            - var columns = ['apple', 'pine', 'peach'];
            tr
                each column in columns
                    th #{column}
            - for (var i = 0; i < 3; i++)
                tr 
                    each column in columns 
                        td #{data[i].column}

here is my app.js 这是我的app.js

const express = require('express');
const pug = require('pug')
var app = module.exports = express();

app.set('view engine', 'pug');

var foo = [{
        apple: 1,
        pine: 2,
        peach: 3
    }, {
        apple: 4,
        pine: 5,
        peach: 6
    },
    {
        apple: 7,
        pine: 8,
        peach: 9
    }
];
app.get('/', (req, res) => {
    res.render('index', {
        data: foo
    });
});

app.listen(3000, () => console.log('Listen to port 3000 now'));

I am not sure how should I do the following trick: inside index.pug I do declare a local js array of three possible values apple , pine and peach , and I am use a for-each loop to iterate over this array. 我不确定如何执行以下操作:在index.pug我声明了一个本地js数组,其中包含三个可能的值applepinepeach ,并且我使用了一个for-each循环来对该数组进行迭代。 However, in the last line of pug, it seems like that pug doesn't substitute the "column" in #{data[i].column} with apple , pine and peach as the loop is iterating over. 但是,在哈巴狗的最后一行中,随着循环的不断循环,哈巴狗似乎没有用applepinepeach代替#{data[i].column}的“列”。

So is there a way to tell pug to replace column in #{data[i].column} with the values of same local js variable that I have declared? 那么,有没有办法告诉#{data[i].column}用我声明的相同本地js变量的值替换#{data[i].column} column

Thanks 谢谢

Check your local variable value and server side variable value is same using if statement and based on that you can give the value to td of local variable column 使用if语句检查您的本地变量值和服务器端变量值是否相同,并基于此值可以将值赋予本地变量column td

table
        - var columns = ['apple', 'pine', 'peach'];
        tr
            each column in columns
                th #{column}
        - for (var i = 0; i < 3; i++)
            tr 
                each column in columns
                    if column == data[i].column
                        td column

Use 采用

html(lang='en')
    head
        title Pug
    body
        table
            - var columns = ['apple', 'pine', 'peach'];
            //- - var columns = Object.keys(data[0]); // Could also use this
            tr
                each column in columns
                    th=column
            - for (var i = 0; i < 3; i++)
                tr 
                    each column in columns
                        td=data[i][column]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM