Twig - 表示宏内部不正确使用宏

时间:2012-08-18 17:43:39

标签: 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 %}
    {% endset %}

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

    {{ size }}

{% endspaceless %}{% endmacro %}

编辑:

感谢您的回答。我最终得到了类似的东西(在浏览器中你有可能不会错过这样的东西):

{% 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 %}

1 个答案:

答案 0 :(得分:3)

您可以打印错误消息,但这是您可以从模板中执行的最佳操作。

如果你想要一个更好的选择,可以编写一个twig扩展,它可以创建一个能够完成你希望宏做的事情的函数。你可以在那里抛出异常。

相关问题