【问题标题】:Express + Jade: render an array of partialsExpress + Jade:渲染一个局部数组
【发布时间】:2014-11-27 10:19:18
【问题描述】:

我有部分button看起来像:

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

我在来自npm install express-partialres.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,避免遇到用逗号分隔的每个按钮。

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

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

示例取自here

【问题讨论】:

  • 我在 4.x 文档中没有看到 res.renderPartials。是从别的地方来的吗?
  • @Scimonster 我在这里找到了它npmjs.org/package/express-partial。请指出我这样做的新方法
  • 不,如果它来自外部模块,那很好。您可能只想在问题中这么说,所以像我这样的人不必去“res.renderPartials 是什么?”
  • @Scimonster no prob - 更新了我的问题

标签: javascript node.js express pug partials


【解决方案1】:

虽然在我的情况下我们可以使用嵌入的部分,例如

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

【讨论】:

  • 这是一种解决方法。它解决了您的特定问题,但不是使用相同模板呈现多个部分的一般问题。
猜你喜欢
  • 2014-01-15
  • 2013-04-15
  • 2014-06-01
  • 1970-01-01
  • 2014-11-10
  • 2015-01-13
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
相关资源
最近更新 更多