将"twig/twig"
更新为"v2.4.4"
后,调用宏函数时出错。
Impossible to invoke a method ("widget_prototype") on a string variable (":ERP/timesheets:_form_part.html.twig").
我的宏:
{% macro widget_prototype(widget, remove_text) %}
{% if widget.vars.prototype is defined %}
{% set form = widget.vars.prototype %}
{% set name = widget.vars.prototype.vars.name %}
{% else %}
{% set form = widget %}
{% set name = widget.vars.full_name %}
{% endif %}
调用宏部分:
{% include ':ERP/timesheets:_data_content_supply_from_widget.html.twig' with {form:form, name:name} %}
{% endmacro %}
<div id="post_supplies"
data-prototype="{{ _self.widget_prototype(form.supplies, 'remove'|trans({}, 'common'))|escape }}"
style="margin-bottom: 5px">
{% for widget in form.supplies.children %}
{{ _self.widget_prototype(widget, 'remove'|trans({}, 'common')) }}
{% endfor %}
</div>
如何正确调用宏?在没有_self
的情况下尝试通话,我收到错误:Unknown "widget_prototype" function.
答案 0 :(得分:3)
DarkBee的答案很好,但如果您的宏位于调用它的同一个twig文件中,那么仍然需要像这样导入它:
{% import _self as my_macros %}
{{ my_macros.widget_prototype(...) }}
似乎有点反直觉,但事实就是如此。
答案 1 :(得分:2)
您需要导入宏,而不是包含它
{% import "my_macro.twig" as my_macro %}
{{ my_macro.function(arg1) }}