Templating code spaces in markdown

I came up with this code that works but it creates spaces between the lines when it writes them out in markdown.

Any ideas?

{% for state in states.input_text %}
  {% if state.entity_id | regex_match('^input_text.lockcode_[0-9]') %}
    {{ state.name | trim }}:{{ state.state | trim }}
  {%- endif %}
{%- endfor %}

This is how they get displayed.
Capture

Maybe try:

{%- for state in states.input_text -%}
  {% if state.entity_id | regex_match('^input_text.lockcode_[0-9]') %}
    {{ state.name | trim }}:{{ state.state | trim }}
  {%- endif %}
{%- endfor %}

@pnbruckner I don’t see any difference between yours and mine?

The dashes in the first line.

Removing some “space” in the code has worked for me earlier with the markdown card.
So you could try removing the double space in the “if” block

{% for state in states.input_text %}
  {% if state.entity_id | regex_match('^input_text.lockcode_[0-9]') %}
  {{ state.name | trim }}:{{ state.state | trim }}
  {%- endif %}
{%- endfor %}

or remove everything (both “if” and “for” block):

{% for state in states.input_text %}
{% if state.entity_id | regex_match('^input_text.lockcode_[0-9]') %}
{{ state.name | trim }}:{{ state.state | trim }}
{%- endif %}
{%- endfor %}

@Kardesken @pnbruckner Actualy, found a solution on a Jinja forum.

It’s all about the dashes in the for loop and if statement.

Here’s my final code. Notice where the dashes are in next to the % in the for loop. I didn’t know those had such an affect.

{% for state in states.input_text -%}
              {% if state.entity_id | regex_match('^input_text.lockcode_[0-9]') %}
                {{ state.name | trim }}:{{ state.state | trim }}
              {%- endif %}
            {%- endfor %}

Capture

Yep, that’s exactly what I suggested, except I thought you didn’t want any blank lines, so I suggested a dash at the beginning of the for line as well.

FYI, you can use these in {{- blah, blah, blah -}} expressions, too. Basically they mean remove white space (which includes EOLs) “in this direction.”

1 Like