我有一个页面树枝,它消耗了一个组件文件,而该组件文件又消耗了另一个文件。我需要为页面文件中的任何这些文件设置所有内容。
在我的页面文件中:
{% set content = {
title: 'page title',
accordion: {
'text': 'TitleA'
}
}
%}
{% include "component.twig" with content %}
在component.twig中:
{% set settings = {
'class': 'my-class',
'style': 'primary'
}|merge(accordion)
%}
{% include "button.twig" with settings %}
在button.twig中:
<button class="{{ class }} button--{{ style }}" type="button">{{ text }}</button>
我正在使用gulp-twig。以上工作,但我在我的控制台中收到此错误。
https://github.com/zimmen/gulp-twig
Error parsing twig template /Users/me/myproject/src/components/component.twig:
TypeError: Cannot read property '_keys' of undefined
at /Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:10:3579
at Array.forEach (native)
at Object.Twig.forEach (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:8:822)
at Twig.filters.merge (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:10:3399)
at Twig.filter (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:10:10126)
at Twig.expression.definitions.parse (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:9:21538)
at /Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:9:28849
at Array.forEach (native)
at Object.Twig.forEach (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:8:822)
at Twig.expression.parse (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:9:28724)
Error parsing twig template /Users/me/myproject/src/components/component.twig:
TypeError: Cannot read property '_keys' of undefined
at /Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:10:3579
at Array.forEach (native)
at Object.Twig.forEach (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:8:822)
at Twig.filters.merge (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:10:3399)
at Twig.filter (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:10:10126)
at Twig.expression.definitions.parse (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:9:21538)
at /Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:9:28849
at Array.forEach (native)
at Object.Twig.forEach (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:8:822)
at Twig.expression.parse (/Users/me/myproject/node_modules/gulp-twig/node_modules/twig/twig.min.js:9:28724)
我可以通过创建以下对象来消除错误。然而奇怪的是,通过查看页面我可以看到TitleA不是TitleB。
{% set accordion = {
'text': 'TitleB'
}
%}
我尝试了不同的语法,例如:
{% include "button.twig" with accordion|merge({class: "my-class", style: "primary"}) %}
答案 0 :(得分:0)
我发现的解决方案是在应用合并过滤器之前将空对象设置为默认值,例如:
{% set foo = foo|default({})|merge(...) %}