Django模板:如何避免空行与包含和加载? [英] Django templates: how to avoid empty lines with include and load?
问题描述
在模板中,我使用以下代码:
In a template, I use the following code:
{% load i18n %}
{% include "header.html" %}
{% include "top_bar.html" %}
但是,当我分开每个包含
和加载时,这会产生几个回车(
通过回车。我不想连接这样的内容: \\\
)
But this produces several carriage returns (\n
) as I separate every include
and load
by a carriage return. I don't want to concatenate inclusions like this:
{% include "header.html" %}{% include "top_bar.html" %}
因为它不可读。
有没有办法避免这些生成的新行?
Is there a way to avoid these generated new lines ?
推荐答案
经过一些研究,我发现这个线程哪里提交了一个补丁并且正在被审查。这个验证步骤对我的项目需要太多时间。
Well after some researches, I found this thread where a patch is submitted and currently being reviewed. This validation step takes too much time regarding my project.
这里是一个django模块我创建了这样做。
Here is a django module I created to do that. It handles whitespaced/blank lines/empty lines just as Gary Wilson asked in the thread above.
它包含一个模板加载器和一个中间件:
It consists in a template loader and a middleware:
- 模板加载器标记所请求的空白行(由开发人员自愿插入)
- 中间件拦截
HttpResponse
并禁止每个空行。然后,它取消以前要求的行,并返回清除的响应
- The template loader tags the requested blank lines (those voluntary inserted by the developer)
- The middleware intercepts the
HttpResponse
and suppresses every blank line. Then, it untags the previously-requested lines and returns the cleared response
这篇关于Django模板:如何避免空行与包含和加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!