Twig not detecting the custom function

469 Views Asked by At

I have written a Twig custom function (in a Twig custom extension). I notice the template does not read the function and keeps throwing me the "Method Exists" error. Was wondering if you have faced this before. Any ideas ?

The custom extension file; $post and $list are both objects:

<?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));
    }

}

and this is where they are being called:

{% 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 %}

and :

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

and this is the screenshot of the error :

error screenshot

0

There are 0 best solutions below