簡體   English   中英

Twig無法在字符串變量上調用方法

[英]Twig impossible to invoke a method on a string variable

"v2.4.4" "twig/twig" "v2.4.4"更新為"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.

DarkBee的答案很好,但是如果您的宏位於調用它的同一個樹枝文件中,則仍然需要像這樣導入它:

{% import _self as my_macros %}
{{ my_macros.widget_prototype(...) }}

似乎有點違反直覺,但這就是事實。

您需要導入宏,而不要包含它

{% import "my_macro.twig" as my_macro %}
{{ my_macro.function(arg1) }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM