I
I
IIITRIX2018-10-18 13:58:55
Django
IIITRIX, 2018-10-18 13:58:55

How to make html code in one line?

In php this is easy to do.
Add a type file to Index

<?php
$s=str_replace("\r\n"," ",$text);
echo preg_replace("/((?<=>)|(?<=--)|(?<=.))[\s\n\r\t]+((?=--)|(?=<))/U",'',$s);?>

How to do it in django?
So that in the browser html is displayed in one line

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tema_sun, 2018-10-18
@tema_sun

The {% spaceless %} tag, although it won't remove all hyphens, because it might break the code. If you need to remove all the transfers directly, then make your tag based on the Jang one.
But I like the filter much more in the nginx pagespeed module:

pagespeed on;
pagespeed EnableFilters collapse_whitespace;

It works much faster, although again, not all transfers are removed. But perhaps this option is also suitable.

V
Vladimir Kuts, 2018-10-18
@fox_12

Yes, pretty much the same:

>>> a = """
... <!DOCTYPE html>
... <html lang="en">
... <head>
...     <meta charset="UTF-8">
...     <title>Title</title>
... </head>
... <body>
... 
... </body>
... </html>
... """

>>> import re
>>> a1 = a.replace("\r\n"," ")
>>> a1
'\n<!DOCTYPE html>\n<html lang="en">\n<head>\n    <meta charset="UTF-8">\n    <title>Title</title>\n</head>\n<body>\n\n</body>\n</html>\n'

>>> re.sub("/((?<=>)|(?<=--)|(?<=.))[\s\n\r\t]+((?=--)|(?=<))/U",'', a1)
'\n<!DOCTYPE html>\n<html lang="en">\n<head>\n    <meta charset="UTF-8">\n    <title>Title</title>\n</head>\n<body>\n\n</body>\n</html>\n'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question