1``spaceless`` 2============= 3 4.. versionadded:: 1.38 5 6 The ``spaceless`` filter was added in Twig 1.38. 7 8Use the ``spaceless`` filter to remove whitespace *between HTML tags*, not 9whitespace within HTML tags or whitespace in plain text: 10 11.. code-block:: jinja 12 13 {{ 14 "<div> 15 <strong>foo</strong> 16 </div> 17 "|spaceless }} 18 19 {# output will be <div><strong>foo</strong></div> #} 20 21You can combine ``spaceless`` with the ``filter`` tag to apply the 22transformation on large amounts of HTML: 23 24.. code-block:: jinja 25 26 {% filter spaceless %} 27 <div> 28 <strong>foo</strong> 29 </div> 30 {% endfilter %} 31 32 {# output will be <div><strong>foo</strong></div> #} 33 34This tag is not meant to "optimize" the size of the generated HTML content but 35merely to avoid extra whitespace between HTML tags to avoid browser rendering 36quirks under some circumstances. 37 38.. caution:: 39 40 As the filter uses a regular expression behind the scenes, its performance 41 is directly related to the text size you are working on (remember that 42 filters are executed at runtime). 43 44.. tip:: 45 46 If you want to optimize the size of the generated HTML content, gzip 47 compress the output instead. 48 49.. tip:: 50 51 If you want to create a tag that actually removes all extra whitespace in 52 an HTML string, be warned that this is not as easy as it seems to be 53 (think of ``textarea`` or ``pre`` tags for instance). Using a third-party 54 library like Tidy is probably a better idea. 55 56.. tip:: 57 58 For more information on whitespace control, read the 59 :ref:`dedicated section <templates-whitespace-control>` of the documentation and learn how 60 you can also use the whitespace control modifier on your tags. 61