0

我写了一个 Twig 自定义函数(在 Twig 自定义扩展中)。我注意到模板没有读取函数并不断向我抛出“方法存在”错误。想知道你以前是否遇到过这种情况。有任何想法吗 ?

自定义扩展文件;$post并且$list都是对象:

<?php

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class ShowContentExtension extends AbstractExtension
{

    public function getFunctions()
    {
        return [
            new TwigFunction('ShowList', [$this, 'ShowList']),
            new TwigFunction('ShowPost', [$this, 'ShowPost'])
        ];
    }

    public function ShowList($list) {
        foreach ($list->post as $post) {
            $this->ShowPost($post);
        }
    }

    public function ShowPost($post) {
        return !((count(array_keys($post->documents))) < 2 && array_key_exists('header', $post->documents));
    }

}

这就是他们被调用的地方:

{% if ShowList(list) %}
    <h2 >{{ title }}</h2>
    <div>
        <div>
            {% for post in list %}
                {% if post|length > 0 %}
                    {% include ./_links.html.twig' with  { 'list': list} %}
                {% endif %}
            {% endfor %}
        </div>
    </div>
    {% endif %}

和 :

{% if ShowPost(post) %}
<div>
    <div>
        <a href="{{ post.link }}">
            <span>{{ post.title }}</span>
        </a>
    </div>
</div>
{% endif %}

这是错误的屏幕截图:

错误截图

4

0 回答 0