缩小HTML输出的简单Django模板加载器。
django-template-minifying-loader的Python项目详细描述
django应用程序,提供简单的模板加载程序。它通过去掉html和django模板标记之间的空白字符来减少模板中的html输出。这是与django 1.10一起工作的django-template-minifier的更新。
注意事项:
- 它不会进行任何花哨的压缩,为此使用GZip Middleware。
- 要压缩css和js,请使用django-compressor。
基本用法
修改django项目设置的模块。
用于生产(注意缓存的加载程序):
TEMPLATES=[{'DIRS':[str(APPS_DIR.path('templates')),],'OPTIONS':{'loaders':['django.template.loaders.cached.Loader','template_minifying_loader.loaders.filesystem.Loader','template_minifying_loader.loaders.app_directories.Loader',],},},]
用于开发(每次刷新重新加载模板):
TEMPLATES=[{'DIRS':[str(APPS_DIR.path('templates')),],'OPTIONS':{'loaders':['template_minifying_loader.loaders.filesystem.Loader','template_minifying_loader.loaders.app_directories.Loader',],},},]
很高兴在你的模板中有更少的空间和新行!
高级用法:
使用修改的设置,您可以: *关闭在HTML标记之间剥离空格
TEMPLATE_MINIFIER_HTML_TAGS=False# default = True
- 关闭django模板标记(s{%,%}s)之间的分隔符
TEMPLATE_MINIFIER_TEMPLATE_TAGS=False# default = True
- 关闭所有剥离功能
TEMPLATE_MINIFIER=False# default = True
- 运行自己的strip_函数,该函数对模板进行预处理
TEMPLATE_MINIFER_STRIP_FUNCTION='template_minifier.utils.strip_spaces_in_template'
(变量名称有误,请参见2了解详细信息)
- 仅在生产中使用
ifDEBUG:TEMPLATE_MINIFIER=False
已知问题:
- 不要在内联javascript<;script>;或.js模板中使用//one line注释。在某些情况下,如果您在那里使用了大量{%if%},它可以注释掉};或},例如:
// comment something - !!it's evil!! {%if%}functionname(){}{%endif%}
使用/**/代替
/* comment something - it's nice and clean <3! */{%if%}functionname(){}{%endif%}
或者只设置template_minifier_template_tags=false
待办事项:
- {%new_line%}模板标记