繁体   English   中英

Express + Jade:渲染局部数组

[英]Express + Jade: render an array of partials

我有部分button看起来像:

.button button.custom_button(type='button', value='', onclick=#{functionName}) #{functionName}

并且我借助来自npm install express-partialres.renderPartials函数渲染了res.renderPartials

我想根据用户要求渲染它。 所以我有类似的控制器方法:

var express = require('express');
var router = express.Router();

router.get('/buttons', function(req, res) {
    var funcs = ['func1();', 'func2();', 'func3()', .... ]; // where funcs.length > 15

    res.renderPartials({    
        // PROBLEM #1: how to make this in a loop
        // PROBLEM #2: why do it returns only the func[28] rendering result
        'partials/button': {functionName: funcs[0]},
        'partials/button': {functionName: funcs[1]},
        ..........
        'partials/button': {functionName: funcs[28]},
    });
});

问题: 如何一次渲染所有按钮局部? 将数组传递给res.renderPartials并避免遇到每个用逗号分隔的按钮。

PS我认为这是可能的,因为在Jade模板中我们可以这样做:

- each video in videos
   !=partial('partials/video', {title:video.title, artist:video.artist})

例子是从这里

我虽然可以使用嵌入式的局部函数,例如

buttons.jade

- each func in #{functions}
   !=partial('partials/button', {functionName:func})

button.jade

.button button.custom_button(type='button', value='', onclick=#{functionName}) #{functionName}

路由器

var express = require('express');
var router = express.Router();

router.get('/buttons', function(req, res) {
    res.renderPartials({    
        'partials/buttons': {functions: ['func1();', 'func2();', 'func3()', .... ]}
    });
});

暂无
暂无

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

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