We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Extra spaces show up when using macro

I have a macro that looks like:

{%- macro image_url(pic_id, big) %}
    {% if big == true %}
        {% set prefix = 2 %}
    {% else %}
        {% set prefix = 1 %}
    {% endif %}
    {% return "https://example.com/images/" ~ prefix ~ "_" ~ pic_id ~ ".jpg" %}
{%- endmacro %}

...and I call this macro as:

<a href="{{image_url(1234, true)}}">

But the resulting HTML shows up as:

<a href="        https://example.com/images/2_1234.jpg">

Why all these extra spaces???



98.9k

You might want to remove extra spaces in the macro:

{%- macro image_url(pic_id, big) -%}
    {%- if big == true -%}
        {%- set prefix = 2 -%}
    {%- else -%}
        {%- set prefix = 1 -%}
    {%- endif -%}
    {%- return "https://example.com/images/" ~ prefix ~ "_" ~ pic_id ~ ".jpg" -%}
{%- endmacro -%}


4.2k

Wow...that actually worked. Thanks so much!

Is that the difference between {% %} and {%- -%}?

Yeah - {%- -%} removes all whitespace.