Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

added {% spaceless %} tag support #193

Merged
merged 1 commit into from
Feb 20, 2013
Merged

added {% spaceless %} tag support #193

merged 1 commit into from
Feb 20, 2013

Conversation

potench
Copy link
Contributor

@potench potench commented Feb 8, 2013

I find the {% spaceless %} tag useful in Django and thought I'd contribute it back as a default tag.

Included:

  • {% spaceless %} tag added as default tag
  • tests for leading, trailing, and "between-tag" whitespace trimming.

Use:

{% spaceless %}
    <li>1</li>
  <li>2</li>
        <li>    </li>
<li> 
4
</li>

{% endspaceless %}
<!--// BECOMES //-->
<li>1</li><li>2</li><li></li><li>
4
</li>
<!--// 
    notice it only removes empty whitespace 
    so "4" retains it's original whitespace 
//-->

Notes

I borrowed the "between-tag" whitespace stripping RegEx >\s+< directly from the Django {% spaceless %} function and added leading and trailing whitespace trimming which doesn't appear to be a function of the Django function (but perhaps should be?)

@paularmstrong
Copy link
Owner

Out of curiosity: what do you find useful about this tag? Most browser's have HTML inspectors now that format the HTML into a nice readable format with "proper" spacing.

Tools like Google mod_pagespeed are much better suited for stripping whitespace from HTML documents, since it properly parses the document and knows where it's okay to remove spaces.

notice it only removes empty whitespace so "4" retains it's original whitespace

Why would I want this? The tag name "spaceless" reads to me like all of the whitespace within the tag will be removed.

@potench
Copy link
Contributor Author

potench commented Feb 9, 2013

  1. minification.
  2. CSS inline-block and flexbox elements respect the non-breaking-space (\r\n) between elements (that extra margin you see between them), unless you like writing like this
<ul><li>
   </li><li>
</li></ul>

@potench
Copy link
Contributor Author

potench commented Feb 11, 2013

Here's the Twig documentation for {% spaceless %}:

This tag is not meant to "optimize" the size of the generated HTML content but merely to avoid extra whitespace between HTML tags to avoid browser rendering quirks under some circumstances.

Understand if you don't want it since this isn't Twig (or Django), just thought I'd send it up since I found myself using it. Maybe it works best as a custom tag though.

Also, just wanted to say thanks for making Swig, it's been super helpful.

@suin
Copy link

suin commented Feb 16, 2013

{% spaceless %} is one of my favorite features of "Django" or "Twig", as it is an easy way for me to minify and obfuscate HTML at the templating layer without special tools like google's pagespeed.

I'm very glad if Swig implements {% spaceless %} :) Thank you.

paularmstrong added a commit that referenced this pull request Feb 20, 2013
added {% spaceless %} tag support
@paularmstrong paularmstrong merged commit c5e83ae into paularmstrong:master Feb 20, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants