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 "http://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="        http://example.com/images/2_1234.jpg">

Why all these extra spaces???



85.3k

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 "http://example.com/images/" ~ prefix ~ "_" ~ pic_id ~ ".jpg" -%}
{%- endmacro -%}


2.5k

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

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

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