簡體   English   中英

在 nunjucks 循環中構建數組/對象

[英]building a array/object in a nunjucks loops

我的 nunjucks 文件中有以下內容,

{% set inProgressJobs = [] %}
{% for job in jobs %}
 {% set inProgressJobs = (setProgressJobs.push([
    {
         text: "<b><a href="">{{ job.jobTitle}}</a></b>"
    }
 ]), inProgressJobs) %}

當我在視圖中使用它時,我希望看到Web Designer但我實際上看到{{ job.jobTitle }}如何將我的 var 插入到文本屬性中?

您嘗試在模板內編碼。 這是可能的(見下文),但超出了模板意識形態。 嘗試將必要的邏輯放在過濾器和全局函數中。

const nunjucks  = require('nunjucks');
const env = nunjucks.configure();

env.addFilter('print', console.log); // for debug

const html = env.renderString(`
    {% set arr = [] %}
    {% for i in range(1, 10) %}
        {{ '' if arr.push(i) }} {# arr.push always returns non-zero so result will be '' #}
    {% endfor %}
    {{ arr | print }} {# print arr to console #}
`);
// console.log(html); // html is a pack of spaces and tabs 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM