Twig Runtime Error: Unable to call method ("test") on string variable

I have the following branch template (the code is in one 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 get the following error:

It is not possible to call a method ("renderJob") in a string variable ("@ AppBundle / Jobs / form / job.html.twig").

I tried to figure out what causes this without luck. This works very well in 1.3.x The fields variable contains the correct data, but it seems that it cannot pass it to the renderJob macro or cannot find the macro (which is odd)?

+6
source share
3 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 %} 
+5
source

I think that itself is deprived of twigg 2.0, maybe you need to check without _self .

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

+2
source

I don't know why it worked before, but you still need to import the macro before you can use it:

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

fiddle

+2
source

Source: https://habr.com/ru/post/1015312/


All Articles