我正在使用带有twig模板的Pattern Lab,并且在包含的with
语句中直接使用对象时遇到问题。
teaser-content-blocks
有机体:{% set content_blocks = [
{
teaser_photo: {
url: 'https://img.buzzfeed.com/buzzfeed-static/static/enhanced/terminal01/2011/3/29/17/enhanced-buzz-14894-1301433714-5.jpg',
alt: 'Man sleeping on a cake',
title: 'Man sleeping on a cake',
height: '200px',
width: '400px',
},
}
] %}
<div class="teaser-content-blocks">
{% for content_block in content_blocks %}
{% include '@molecules/teaser-content-block.twig' with content_block only %}
{% endfor %}
</div>
teaser-photo
原子:<div class="teaser-photo">
<img
src="{{ url }}"
alt="{{ alt }}"
title="{{ title }}"
/>
</div>
teaser-content-block
分子:<div class="teaser-content-block">
<div class="left">
{% include '@atoms/teaser-photo.twig' with teaser_photo only %}
</div>
<div class="right">
{% include '@atoms/title.twig' with { title: 'Sleep on a cake', element: 'h3' } only %}
{% include '@atoms/teaser-body.twig' with { body: "When we say 'sweet dreams', this isn't quite what we mean! Check this mad lad not understanding the general concept of pillows! This utter banterboy has completely confused what he is meant to use as a confectionary-based celebration of one's birth and a soft item of bedding designed to cushion the head during sleep or light rest. Mental!" } only %}
</div>
</div>
然而,似乎with teaser_photo only
语句导致twig断开而不能编译,错误为Catchable fatal error: Argument 1 passed to Twig_Template::display() must be of the type array, null given
。
如果我将include语句更改为以下内容:
{% include '@atoms/teaser-photo.twig' with { url: teaser_photo.url, alt: teaser_photo.alt, title: teaser_photo.title } only %}
......它运作正常,但这对我来说太冗长了。我宁愿把对象原样传递下来。它似乎不想以正确的方式提取它,即使理论上它应该是相同的。
我不是一个树枝专家,所以我可能会遗漏一些非常明显的东西。
任何帮助都一如既往地受到赞赏!
答案 0 :(得分:0)
发现了这个问题。 Pattern Lab当然会尝试渲染每个可用的模式,因此它试图渲染teaser-content-block.twig
,它试图引用变量teaser_photo
,这当然不存在于此范围内。因此,解决方法是使用默认值替换该行,如下所示:
{% include '@atoms/teaser-photo.twig' with teaser_photo|default({}) only %}
......或者至少做类似事情的事情。感谢@DarkBee也花时间去看看。