Is there a good way to indicate incorrect use of Twig macro from inside a macro?
For example:
{% 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 %}
Edit:
Thank you for your answers. I have ended up with something like this (there is a chance you will not miss something like this in the browser):
{% 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 %}
You can print an error message, but that’s the best you can do from template.
If you want a better alternative, write a twig extension which creates a function that does what you want your macro to do. You can throw exceptions there.