Macro output format problem. Help, please?

Hello. I am trying to use the new macros, bur i’m stuck on the output.
The first macro should return a normal string, and the second one should receive input from the first to output another string. All good, but the output format of the macros, in strings, somehow get weird characters inside, so the second one never gets the right result.

Code (you can test right in the template editor):

{% macro motivo_portas2(temp_int,temp_ext,temp_opt,temp_tol,humi_int,humi_ext,humi_opt,humi_tol) %}
  {% set temp_tol_min = temp_tol / 2 %}
  {% set humi_tol_min = humi_tol / 2 %}
  {% set motivo = '' %}
  {% if temp_ext < temp_opt %}
    {% if (temp_opt - temp_ext) >= temp_tol %}
      {% if temp_ext <= temp_int %}
        {% if temp_int - temp_ext >= temp_tol_min %}
          {% set motivo = 'mais frio no exterior' %}
        {% else %}
          {% set motivo = '' %}
        {% endif %}
      {% else %}
        {% if temp_ext - temp_int >= temp_tol_min %}
          {% set motivo = 'muito frio no interior' %}
        {% else %}
          {% set motivo = '' %}
        {% endif %}
      {% endif %}
    {% elif (temp_opt - temp_ext) >= temp_tol_min %}
      {% if temp_ext <= temp_int %}
        {% if temp_int - temp_ext >= temp_tol_min %}
          {% set motivo = 'mais frio no exterior' %}
        {% else %}
          {% set motivo = '' %}
        {% endif %}
      {% else %}
        {% if temp_ext - temp_int >= temp_tol_min %}
          {% set motivo = 'muito frio no interior' %}
        {% else %}
          {% set motivo = '' %}
        {% endif %}
      {% endif %}
    {% else %}
      {% set motivo = '' %}
    {% endif %}
  {% else %}
    {% if (temp_ext - temp_opt) >= temp_tol %}
      {% if temp_ext >= temp_int %}
        {% if temp_ext - temp_int >= temp_tol_min %}
          {% set motivo = 'mais calor no exterior' %}
        {% else %}
          {% set motivo = '' %}
        {% endif %}
      {% else %}
        {% if temp_int - temp_ext >= temp_tol_min %}
          {% set motivo = 'muito calor no interior' %}
        {% else %}
          {% set conselho = indiferente %}
        {% endif %}
      {% endif %}
    {% elif (temp_opt - temp_ext) >= temp_tol_min %}
      {% if temp_ext >= temp_int %}
        {% if temp_ext - temp_int >= temp_tol_min %}
          {% set motivo = 'mais calor no exterior' %}
        {% else %}
          {% set conselho = indiferente %}
        {% endif %}
      {% else %}
        {% if temp_int - temp_ext >= temp_tol_min %}
          {% set motivo = 'muito calor no interior' %}
        {% else %}
          {% set motivo = '' %}
        {% endif %}
      {% endif %}
    {% else %}
      {% set motivo = '' %}
    {% endif %}
  {% endif %}
  {% if motivo == '' %}
    {% if humi_int - humi_ext >= humi_tol %}
      {% if humi_int - humi_opt >= humi_tol_min %}
        {% set conselho = abrir %}
        {% set motivo = 'muito húmido no interior' %}
      {% endif %}
    {% elif humi_ext - humi_int >= humi_tol %}
      {% if humi_int - humi_opt >= humi_tol_min %}
        {% set conselho = abrir %}
        {% set motivo = 'muito seco no interior' %}
      {% endif %}
    {% endif %}
  {% endif %}
  {{motivo}}
{% endmacro %}

{% macro conselho_portas2(motivo) %}
  {% set abrir = 'ABRIR' %}
  {% set fechar = 'FECHAR' %}
  {% set indiferente = 'Indiferente' %}
  {% set conselho = indiferente %}
  {% if motivo == 'muito húmido no interior' %}
    {% set conselho = abrir %}
  {% elif motivo == 'muito seco no interior' %}
    {% set conselho = abrir %}
  {% elif motivo == 'mais frio no exterior' %}
    {% set conselho = fechar %}
  {% elif motivo == 'mais calor no exterior' %}
    {% set conselho = fechar %}
  {% elif motivo == 'muito frio no interior' %}
    {% set conselho = abrir %}
  {% elif motivo == 'muito calor no interior' %}
    {% set conselho = abrir %}
  {% else %}
    {% set conselho = indiferente %}
  {% endif %}
  {{conselho}}
{% endmacro %}

{# String from first macro seems alright: #}
{{motivo_portas2(56,56,12,45,70,80,45,9)}}
{# But in fact it is not if I insert it in a map: #}
{{[motivo_portas2(56,56,12,45,70,80,45,9)]}}
{# I can correct it after the output of the macro: #}
{{[motivo_portas2(56,56,12,45,70,80,45,9)|replace('\n','')|replace('  ','')|replace('  ','')]}}
{# but not inside the macro, if I try to do these |replace in the macro output the result is the same#}

{# If I try to feed the result of first macro directly to the second macro, the matching string is never found #}
{{ conselho_portas2(motivo_portas2(56,56,12,45,70,80,45,9)) }}
{# if I filter the result of the first macro before sending to the second, it seems to work alright #}
{{ conselho_portas2(motivo_portas2(56,56,12,45,70,80,45,9)|replace('\n','')|replace('  ','')|replace('  ',''))}}
{# bun in fact, also the result of the second macro has this weird 'escapes', that i can see if I insert it in a map#}
{{[conselho_portas2(motivo_portas2(56,56,12,45,70,80,45,9)|replace('\n','')|replace('  ','')|replace('  ',''))]}}

(please paste this in template editor in developer tools to test)
this is my output:

muito seco no interior


['\n  \n  \n  \n  \n    \n      \n    \n  \n  \n    \n      \n        \n        \n      \n    \n  \n  muito seco no interior\n']

['muito seco no interior']




  
  
  
  
  
    
  
  Indiferente



  
  
  
  
  
    
  
  ABRIR


['\n  \n  \n  \n  \n  \n    \n  \n  ABRIR\n']

What Im I doing wrong?
Appreciate any guidance! Thanks in advance!

Well, if I remove all spacing, identation and line change from the macros it works as expected, but it’s impratical to maintain and improve a complex macro all in 1 line…

{% macro motivo_portas2(temp_int,temp_ext,temp_opt,temp_tol,humi_int,humi_ext,humi_opt,humi_tol) %}{% set temp_tol_min = temp_tol / 2 %}{% set humi_tol_min = humi_tol / 2 %}{% set motivo = '' %}{% if temp_ext < temp_opt %}{% if (temp_opt - temp_ext) >= temp_tol %}{% if temp_ext <= temp_int %}{% if temp_int - temp_ext >= temp_tol_min %}{% set motivo = 'mais frio no exterior' %}{% else %}{% set motivo = '' %}{% endif %}{% else %}{% if temp_ext - temp_int >= temp_tol_min %}{% set motivo = 'muito frio no interior' %}{% else %}{% set motivo = '' %}{% endif %}{% endif %}{% elif (temp_opt - temp_ext) >= temp_tol_min %}{% if temp_ext <= temp_int %}{% if temp_int - temp_ext >= temp_tol_min %}{% set motivo = 'mais frio no exterior' %}{% else %}{% set motivo = '' %}{% endif %}{% else %}{% if temp_ext - temp_int >= temp_tol_min %}{% set motivo = 'muito frio no interior' %}{% else %}{% set motivo = '' %}{% endif %}{% endif %}{% else %}{% set motivo = '' %}{% endif %}{% else %}{% if (temp_ext - temp_opt) >= temp_tol %}{% if temp_ext >= temp_int %}{% if temp_ext - temp_int >= temp_tol_min %}{% set motivo = 'mais calor no exterior' %}{% else %}{% set motivo = '' %}{% endif %}{% else %}{% if temp_int - temp_ext >= temp_tol_min %}{% set motivo = 'muito calor no interior' %}{% else %}{% set conselho = indiferente %}{% endif %}{% endif %}{% elif (temp_opt - temp_ext) >= temp_tol_min %}{% if temp_ext >= temp_int %}{% if temp_ext - temp_int >= temp_tol_min %}{% set motivo = 'mais calor no exterior' %}{% else %}{% set conselho = indiferente %}{% endif %}{% else %}{% if temp_int - temp_ext >= temp_tol_min %}{% set motivo = 'muito calor no interior' %}{% else %}{% set motivo = '' %}{% endif %}{% endif %}{% else %}{% set motivo = '' %}{% endif %}{% endif %}{% if motivo == '' %}{% if humi_int - humi_ext >= humi_tol %}{% if humi_int - humi_opt >= humi_tol_min %}{% set conselho = abrir %}{% set motivo = 'muito húmido no interior' %}{% endif %}{% elif humi_ext - humi_int >= humi_tol %}{% if humi_int - humi_opt >= humi_tol_min %}{% set conselho = abrir %}{% set motivo = 'muito seco no interior' %}{% endif %}{% endif %}{% endif %}{{motivo}}{% endmacro %}

Help, please?

Use -%} at the end of lines to suppress whitespace. See here:

https://jinja.palletsprojects.com/en/latest/templates/#whitespace-control

THANK YOU! :D. Never really understood the difference until now :).
GREAT! :slight_smile:

1 Like