Django 模板格式化导致“参差不齐” html
为了方便起见,我在模板中使用了一些格式(换行符、间距等),例如:
{% extends "base.html" %}
{% load tabs %}
{% block content %}
{% block navigation %}
<ul id="user_admin_tabs_list">
<li><a href="{% url user_admin.views.profile %}" class=
{% ifactivetab "user_admin_tabs" "profile" %}
"user_admin_tabs_active_tab"
{% else %}
"user_admin_tabs_inactive_tab"
{% endifactivetab %}>Профиль</a></li>
...
但这会导致“参差不齐”的 html 输出。像这样:
<ul id="user_admin_tabs_list">
<li><a href="/accounts/profile/profile/" class=
"user_admin_tabs_active_tab"
>Профиль</a></li>
<li><a href="/accounts/profile/shops/" class=
"user_admin_tabs_inactive_tab"
>Магазины</a></li>
<li><a href="/accounts/profile/billing/" class=
"user_admin_tabs_inactive_tab"
>Биллинг</a></li>
<li><a href="/accounts/profile/settings/" class=
"user_admin_tabs_inactive_tab"
>Настройки</a></li>
</ul>
因此模板的可读性会导致输出 html 的可读性较差。
这个问题的决定是什么?
To my convenience I use some formatting in my templates (line breaks, spacing, etc.) For example:
{% extends "base.html" %}
{% load tabs %}
{% block content %}
{% block navigation %}
<ul id="user_admin_tabs_list">
<li><a href="{% url user_admin.views.profile %}" class=
{% ifactivetab "user_admin_tabs" "profile" %}
"user_admin_tabs_active_tab"
{% else %}
"user_admin_tabs_inactive_tab"
{% endifactivetab %}>Профиль</a></li>
...
But this leads to "ragged" html output. Like this:
<ul id="user_admin_tabs_list">
<li><a href="/accounts/profile/profile/" class=
"user_admin_tabs_active_tab"
>Профиль</a></li>
<li><a href="/accounts/profile/shops/" class=
"user_admin_tabs_inactive_tab"
>Магазины</a></li>
<li><a href="/accounts/profile/billing/" class=
"user_admin_tabs_inactive_tab"
>Биллинг</a></li>
<li><a href="/accounts/profile/settings/" class=
"user_admin_tabs_inactive_tab"
>Настройки</a></li>
</ul>
Therefore readability of templates causes bad readability of output html.
What is the decision of this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
老实说,我不会担心模板引擎的输出。您将编辑模板,而不是输出,因此为了可维护性,只有 模板 HTML 结构良好才真正重要。当然,如果有人在您的网页上使用“查看源代码”,他们会看到一堆混乱的 HTML,但这并不重要。
也就是说,您可以尝试使用 Django 的
spaceless
标签 以获得更漂亮的 HTML 输出。编辑
对于这种特定情况,您可以将
ifactivetab
标记内联:To be honest, I wouldn't worry about the output of the template engine. You're going to be editing the templates, not the output, so for maintainability, it only really matters if the template HTML is well-structured. Sure, if someone uses "View Source" on your webpage, they'll see a mess of HTML, but that doesn't really matter that much.
That said, you could try using Django's
spaceless
tag to get prettier HTML output.Edit
For this specific case, you could put the
ifactivetab
tags inline:我过去也考虑过这个问题。
IMO 最好的选择是使用
tidy
之类的东西对输出进行后处理。它是用 C 语言编写的,因此速度相当快,并且不会对性能造成太大影响。这是开发中的一个选项,但不是您希望在高流量网站上进行生产的选项。您需要将其配置为仅缩进代码。
另一方面,我认为它可能会尝试清理无效的标记,这会让您在启用/禁用它时摸不着头脑。需要注意的事项,因为文档的结构可能会因此而改变。
如果您“查看源代码”,这很好,但您最好还是使用 firebug 来检查您的输出 - 如果这是动机的话。
I've thought about this in the past.
Your best bet IMO is to post process the output with something like
tidy
. It's written in C so it's pretty fast and won't incur much of a performance hit. It's an option in development but not something you want in production on a high traffic site.You'll want to configure it to only indent code.
Off hand, I think it might try to clean up invalid markup which'll leave you scratching your head when you enable/disable it. Something to be aware of because the structure of your document might change as a result.
It's kind of nice if you're "viewing source" but you're probably better off just using firebug to examine your output anyway - if that's the motivation.