将 Timber::get_posts 与 include 一起使用时请注意
Notice when using Timber::get_posts together with an include
我有这个代码:
Twig 模板:
<div class="content-items-wrapper">
{% for item in home.latest_posts %}
{% include "partials/content-item.twig" with item %}
{% endfor %}
</div>
在我的主题文件中:
$context['home']['latest_posts'] = new Timber::get_posts([
'posts_per_page' => 6,
]);
当使用这行代码时,我收到以下通知:
Warning: array_merge(): Argument #2 is not an array in //content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 86
随后出现致命错误:
Catchable fatal error: Argument 1 passed to Twig_Template::display() must be of the type array, null given, called in /content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 86 and defined in //content/plugins/timber-library/vendor/twig/twig/lib/Twig/Template.php on line 401
奇怪的是:当我不使用Twig模板中的include,而是直接使用这个include的内容时,错误就没有了。
此外,仅在包含的部分中放入一个字符(甚至什么都不放)也会产生错误。
另外,当不使用 Timber::get_posts()
,而只是用正确的数据填充数组时,我没有收到任何错误。
但显然两者都不是真正想要的解决方案:-)
这会是什么?
PS:我有一个仅限前端的版本(gulp-twig 的 运行),由 JSON 数据提供,这只是 运行很好。
编辑:修复了示例(copy/paste 错误)
您不需要像 $context['home']
或 $context['home']['latest_posts']
添加上下文 home.items
就像 $context['posts']
一样吗?
问题解决了!
改变
{% include "partials/content-item.twig" with item %}
变成 {% include "partials/content-item.twig" with { 'item' : item } %}
就成功了。
我有这个代码:
Twig 模板:
<div class="content-items-wrapper">
{% for item in home.latest_posts %}
{% include "partials/content-item.twig" with item %}
{% endfor %}
</div>
在我的主题文件中:
$context['home']['latest_posts'] = new Timber::get_posts([
'posts_per_page' => 6,
]);
当使用这行代码时,我收到以下通知:
Warning: array_merge(): Argument #2 is not an array in //content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 86
随后出现致命错误:
Catchable fatal error: Argument 1 passed to Twig_Template::display() must be of the type array, null given, called in /content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 86 and defined in //content/plugins/timber-library/vendor/twig/twig/lib/Twig/Template.php on line 401
奇怪的是:当我不使用Twig模板中的include,而是直接使用这个include的内容时,错误就没有了。 此外,仅在包含的部分中放入一个字符(甚至什么都不放)也会产生错误。
另外,当不使用 Timber::get_posts()
,而只是用正确的数据填充数组时,我没有收到任何错误。
但显然两者都不是真正想要的解决方案:-)
这会是什么?
PS:我有一个仅限前端的版本(gulp-twig 的 运行),由 JSON 数据提供,这只是 运行很好。
编辑:修复了示例(copy/paste 错误)
您不需要像 $context['home']
或 $context['home']['latest_posts']
添加上下文 home.items
就像 $context['posts']
一样吗?
问题解决了!
改变
{% include "partials/content-item.twig" with item %}
变成 {% include "partials/content-item.twig" with { 'item' : item } %}
就成功了。