templatel icon indicating copy to clipboard operation
templatel copied to clipboard

Ignore whitespace around templatel tags

Open markokocic opened this issue 4 years ago • 2 comments

If a line inside a template contains only templatel directives and whitespace, the complete line should be removed from the output when rendered.

Use case 1:

template for RSS xml feed:

{# feed.xml; -*- Mode: jinja2; -*- #}
<?xml version="1.0" encoding="UTF-8" ?>

is rendered like


<?xml version="1.0" encoding="UTF-8" ?>

Note the empty line at the begining is making this file invalid xml file.

Use case 2:

      <ul>
        {% for post in posts %}
          <li>
            <section>
                <h3>{{ post.date|strftime(date_format) }} - <a href="{{ post.url }}">{{ post.title }}</a></h3>
                {% if post.description %}<p>{{ post.description }}</p>{% endif %}
            </section>
          </li>
        {% endfor %}
      </ul>

this renders the html output like this:

        
          <li>
            <section>
                <h3>2019-02-22 - <a href="">title 1</a></h3>
                <p>description</p>
            </section>
          </li>
        
          <li>
            <section>
                <h3>2016-11-22 - <a href="">title 2</a></h3>
                
            </section>
          </li>
        
          <li>
            <section>
                <h3>2016-01-04 - <a href="">title 3</a></h3>
                
            </section>
          </li>
        

While the html is valid, it looks ugly with all those empty lines.

As an (aesthetical) enhancement it would be good if templatel would not render at all line that contains only templatel tags and whitespace.

markokocic avatar Mar 18 '21 14:03 markokocic

yeah, it'd be great to support what jinja calls whitespace-control!

clarete avatar Apr 10 '21 04:04 clarete

I also bump into this issue, would be great to have this support.

I also tried something like {% if abc -%} but it says invalid syntax..

bump the request

emacsbliss avatar Jan 29 '24 03:01 emacsbliss