有没有一种从宏内部指示错误使用 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 %}