0

with我正在将 Pattern Lab 与树枝模板一起使用,并且在包含的语句中直接使用对象时遇到问题。

这是我的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 '@atoms/teaser-photo.twig' with { url: teaser_photo.url, alt: teaser_photo.alt, title: teaser_photo.title } only %}

...它工作正常,但这对我来说太冗长了。我宁愿将对象按原样传递。它似乎只是不想以正确的方式拾取它,即使理论上它应该工作相同。

我不是树枝专家,所以我可能遗漏了一些非常明显的东西。

一如既往地感谢任何帮助!

4

1 回答 1

0

发现了问题。Pattern Lab 当然会尝试渲染每个可用的模式,所以它试图渲染teaser-content-block.twig试图引用变量teaser_photo的,当然这个范围内不存在。所以解决方法是用使用默认值的行替换该行,如下所示:

{% include '@atoms/teaser-photo.twig' with teaser_photo|default({}) only %}

...或者至少做类似的事情。也感谢@DarkBee 花时间看一看。

于 2017-05-24T09:17:29.633 回答