Php 细枝-从宏内部指示宏使用不正确

Php 细枝-从宏内部指示宏使用不正确,php,twig,template-engine,Php,Twig,Template Engine,有没有一种好方法可以从宏内部指示Twig宏的错误使用 例如: {% macro _name_to_size(input) %}{% spaceless %} {% set size %} {% if input == 'small' %}20{% endif %} {% if input == 'medium' %}40{% endif %} {% if input == 'large' %}60{% endif %} {% en

有没有一种好方法可以从宏内部指示Twig宏的错误使用

例如:

{% macro _name_to_size(input) %}{% spaceless %}

    {% set size %}
        {% if input == 'small' %}20{% endif %}
        {% if input == 'medium' %}40{% endif %}
        {% if input == 'large' %}60{% endif %}
    {% endset %}

    {% if not size|trim|length %}
        {# tell user that something is wrong #}
    {% endif %}

    {{ size }}

{% endspaceless %}{% endmacro %}
编辑:

谢谢你的回答。我最终得到了这样的结果(在浏览器中您可能不会错过这样的结果):

{%宏错误(消息)%}
{%set通知%}
{{message}}
{%endset%}
{{通知}}
var messageDiv=document.createElement(“div”);
messageDiv.innerHTML=“{{notification | e('js')}}”;
document.body.appendChild(messageDiv);
{%endmacro%}

您可以打印错误消息,但这是从模板中可以做到的最好的方法

如果您想要一个更好的替代方案,请编写一个细枝扩展,它可以创建一个函数来执行您希望宏执行的操作。您可以在那里抛出异常。

谢谢。:)我将创建宏“error”,它将打印带有错误消息的大红色内联样式框。
{% macro error(message) %}
    {% set notification %}
        <div style="
            background-color: {{ background }} !important;
            display: block !important;
            height: 100% !important;
            left: 0 !important;
            overflow: none !important;
            position: fixed !important;
            top: 0 !important;
            width: 100% !important;
            z-index: 1000 !important;
        ">
            <div style="
                background-color: {{ foreground }} !important;
                color: white !important;
                display: block !important;
                font-size: 4em !important;
                line-height: 1.6em !important;
                left: 0 !important;
                position: relative !important;
                text-align: center !important;
                top: 32% !important;
                z-index: 1100 !important;
            ">{{ message }}</div>
        </div>
    {% endset %}
    {{ notification }}
    <script>
        var messageDiv = document.createElement("div");
        messageDiv.innerHTML = "{{ notification|e('js') }}";
        document.body.appendChild(messageDiv);
    </script>
{% endmacro %}