简体   繁体   English

Express + Jade:渲染局部数组

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

I have partial button that looks like: 我有部分button看起来像:

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

And I render partials with help of res.renderPartials function which comes from npm install express-partial . 并且我借助来自npm install express-partialres.renderPartials函数渲染了res.renderPartials

And I want to render it on user request. 我想根据用户要求渲染它。 So I have controller method like: 所以我有类似的控制器方法:

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]},
    });
});

Question: how to render all button partials at once? 问题: 如何一次渲染所有按钮局部? Mean pass array to res.renderPartials and avoid encountering each button separated by comma. 将数组传递给res.renderPartials并避免遇到每个用逗号分隔的按钮。

PS I supposed that's possible because in Jade template we can do this: PS我认为这是可能的,因为在Jade模板中我们可以这样做:

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

Example is taken from here 例子是从这里

I though in my case we can use embedded partials, like 我虽然可以使用嵌入式的局部函数,例如

buttons.jade buttons.jade

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

button.jade button.jade

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

router 路由器

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