Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig Runtime Error: Impossible to invoke a method ("test") on a string variable

Tags:

twig

symfony

I have the following twig template (the code is in the same file):

{% macro renderJob(fields) %}
    // renders the job UI block, but I've removed it for simplicity
    Hello world.
{% endmacro %}
{% block _jobs_widget %}
    <div id="jobsContainer">
        {% for fields in form.children %}
            {% dump fields %}
            {{ _self.renderJob(fields) }}
        {% endfor %}
    </div>
{% endblock %}

For some reason, after upgrading to twig/twig = v2.1.0 I'm receiving the follwing error:

Impossible to invoke a method ("renderJob") on a string variable ("@AppBundle/Jobs/form/job.html.twig").

I have been trying to figure out what's causing this without any luck. This used to work just fine in 1.3.x. The fields variable contains the proper data, but it appears it can't pass it to the renderJob macro or it can't find the macro (which is kind of odd)?

like image 774
tftd Avatar asked Feb 27 '17 10:02

tftd


2 Answers

Have you tried the following ?

{% import _self as renderJobMacro %}

{% macro renderJob(fields) %}
    // renders the job UI block, but I've removed it for simplicity
    Hello world.
{% endmacro %}

{% block _jobs_widget %}
    <div id="jobsContainer">
        {% for fields in form.children %}
            {{ renderJobMacro.renderJob(fields) }}
        {% endfor %}
    </div>
{% endblock %}
like image 55
Laurent W. Avatar answered Sep 20 '22 11:09

Laurent W.


I think _self is depricated from twigg 2.0, May be you need to check without _self.

Check {{ renderJob(fields) }} instead of {{ _self.renderJob(fields) }}

like image 29
Maulik Savaliya Avatar answered Sep 17 '22 11:09

Maulik Savaliya