Forum

Thread tagged as: Error

Unwanted white space added when using if's in template file.

I noticed that when I use if's and else's in my templates, the space they occupy is rendered out as massive white space in the source files.

Not that this is a big problem, but none the less seems like a bug.

Jarl Robert Kristiansen

Jarl Robert Kristiansen 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you give an example?

This is part of a template I am working on:

<section class="slide">
    <div class="grid wide wide-desktop center">
<perch:if exists="slide_bilde">     
<perch:if exists="slide_use_coloumns">
            <figure class="col-6">
<perch:else/>
            <figure class="center" style="max-width: 750px;">
</perch:if>
                    <a class="no-default-animation" href="<perch:content id="slide_bilde" type="image" label="Bilde"/>" rel="lightbox"><img width="100%" src="<perch:content id="slide_bilde" type="image" label="Bilde"/>" alt="<perch:content id="bilde_alt" type="text" label="Bilde beskrivelse"/>"/></a>
<perch:content type="checkbox" id="slide_use_coloumns" value="1" label="Bilde og tekst er sidestillt." suppress="true"/>
<perch:if exists="slide_use_coloumns">
            </figure>
<perch:else/>
            </figure><br>
</perch:if>
</perch:if>
<perch:if exists="slide_text">
<perch:if exists="slide_use_coloumns">
            <article class="col-6"> 
<perch:else/>
            <article class="col-12">
</perch:if>
<perch:if exists="slide_headline">
                    <h3><perch:content id="slide_headline" title="true" label="Tittel" /></h3>
</perch:if>
                    <perch:content id="slide_text" type="textarea" label="Tekst" markdown="true" editor="markitup" />
            </article>
</perch:if>
    </div>
</section>

All those '<perch:if>' statements are rendered as newlines in the resulting html source.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You've placed them on new lines. Perch only removes its own tags - so if they're on a new line the tag will go and the new line remains.

Ok, fair enough. I guess you are not parsing the whole html file, but rather do a search and replace for any perch tags. If you do parse the whole html file, couldn't you remove those newlines between ordinary html and the perch tags?

It can quickly get confusing with all those tags if they are all on the same line.

Thanks!

Drew McLellan

Drew McLellan 2638 points
Perch Support

I have no way of knowing if the whitespace is significant in the context. You're presuming HTML, and a lot of the time it is HTML, but not always.

If I was removing whitespace, I probably wouldn't in HTML as whitespace is not usually significant in HTML and will be compressed out with gzip anyway. So safer not to try to be too clever with it.

There are some instances in HTML where whitespace is significant ... where having some whitespace vs. no whitespace will end up rendering unwanted space in the rendered page.

The Twig templating engine has a clever way of optionally enabling whitespace gobbling ... https://twig.sensiolabs.org/doc/templates.html#templates-whitespace-control

Is there a way to do something similar in Perch, when/if it's wanted? (ie., when wanting to keep whitespace in a template for readability, but remove excess from the output).

Probably not the biggest deal in the world, but it can come in handy sometimes.