I use Django template system to do code generation (not only for HTML). I'm somehow troubled by redundant line breaks in django templates.
Here is an example. The template is as below:
// something
{% for element in elements %}
Element: {{ element.name }},
{% endfor %}
// something else
The rendered output will be:
// something
Element: foo
Element: bar
// something else
Expected rendered output should be:
// something
Element: foo
Element: bar
// something else
After googled a bit, I know I can use {% spaceless %}
to remove any white spaces in rendered output. It is quite useful for HTML, but will not work for other languages. My current solution is to add a special string after a tag and replace them with empty string in output.
Is there any better solution to remove line break after a tag?
For your production environment you might consider minifying your html to get that little bit more performance. For example using https://pypi.python.org/pypi/django-htmlmin.
If you are only interested in the esthetics, then the .strip
function as noted by e-nouri is probably your best answer.