Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent space in output due to line break in the HTML code?

Tags:

html

jinja2

I have seen this question up here and some are solving it by comment tags or breaking tags like this:

 <i><!--
-->t does not work on the first level with tabspace = 2
   beside it looks horrible.  
<a
 >nnoying<
/a> is it not?

I remember as if I have seen some trick like

<a>&dont_put_here_space_please;
  tricky ampersand code.

Is there any such thing?

I am using now jinja2 python template engine. Does it have some space-preventing trick?

UPDATE:

With jinja2, thanks to dav1d, the shortest way I could come up with is:

<a>{% if true -%}
  No space in the output before this text.
</a>{% endif -%}
  Nor after.

Is there any shorter way of doing this?

like image 988
Barney Szabolcs Avatar asked May 06 '13 16:05

Barney Szabolcs


People also ask

How do you preserve spaces and line breaks in HTML?

The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks.

How do I get rid of space in HTML?

The trim() method removes whitespace from both sides of a string.

How do you ignore a line break in HTML?

Basic HTML Line Break Syntax You can insert line breaks in HTML with the <br> tag, which is equivalent to a carriage return on a keyboard. Be aware that HTML will ignore any line break from a keyboard's return key.

How do you preserve space in HTML?

The HTML <pre> tag defines preformatted text preserving both whitespace and line breaks in the HTML document. This tag is also commonly referred to as the <pre> element.


2 Answers

Jinja2 WhiteSpace Control is what you need (according to your reponse to my comment): http://jinja.pocoo.org/docs/templates/#whitespace-control

like image 177
dav1d Avatar answered Oct 18 '22 21:10

dav1d


This should do what you want:

{# -#}
like image 29
maedox Avatar answered Oct 18 '22 21:10

maedox