Forum
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.
Can you give an example?
This is part of a template I am working on:
All those '<perch:if>' statements are rendered as newlines in the resulting html source.
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!
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.