Python Jinja 模板是否有惯用的文件扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29590931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an idiomatic file extension for Jinja templates?
提问by Chris Tonkinson
I need to programatically distinguish between Jinja template files, other template files (such as ERB), and template-less plain text files.
我需要以编程方式区分 Jinja 模板文件、其他模板文件(例如 ERB)和无模板纯文本文件。
According to Jinja documentation:
根据Jinja 文档:
A Jinja template doesn't need to have a specific extension: .html, .xml, or any other extension is just fine.
Jinja 模板不需要有特定的扩展名:.html、.xml 或任何其他扩展名就可以了。
But what should I use when an explicit extension is required? .py
is misleading, and any search including the words "jinja" and "extension" are badly searchwashed by discussion around Jinja Extensions.
但是当需要显式扩展时我应该使用什么?.py
具有误导性,任何包括“jinja”和“extension”在内的搜索都会被围绕Jinja Extensions的讨论严重洗刷。
I could easily dictate a project-wide convention (.jnj
or .ja
come to mind) but this is for open source so I don't want to buck the trend if there's already established practice somewhere.
我可以轻松地规定一个项目范围的约定(.jnj
或.ja
想到),但这是针对开源的,所以如果某处已经建立了实践,我不想逆势而上。
EDIT 1:Again, I understand that the Jinja project — purposefully — does not define a default file extension. I'm asking if there are any unofficial conventions that have emerged for circumstances where one is desired for some project-specific reason.
编辑 1:再次,我明白 Jinja 项目 - 有目的地 - 没有定义默认文件扩展名。我在问是否有任何非官方约定出现在出于某些特定项目原因需要的情况下。
EDIT 2:Clarification: This is not for HTML content.
编辑 2:澄清:这不适用于 HTML 内容。
采纳答案by bigOther
Jinja Authors did not define a default extension. Most of Jinja template editors like Vim extension, TextMate extension, Emacs extension, and PyCharmmention no default extension to enforce Jinja highlighting.
Jinja Authors 没有定义默认扩展名。大多数 Jinja 模板编辑器,如Vim 扩展、TextMate 扩展、Emacs 扩展和PyCharm 都没有提到强制执行 Jinja 突出显示的默认扩展。
Django had already a similar debateabout setting a default extension, and ended as a wontfixissue. I quote from the closing message:
Django 已经就设置默认扩展进行了类似的辩论,并以一个不会修复的问题结束。我引用结束语:
Filetype detection based on extension is flawed for the very reasons described in these comments, so you have to do some internal inspection, just like MIME type detection works.
由于这些评论中描述的原因,基于扩展名的文件类型检测存在缺陷,因此您必须进行一些内部检查,就像 MIME 类型检测工作一样。
I suggest that you should use your own since there is no common one.
我建议你应该使用自己的,因为没有通用的。
Update:Things changed since I wrote this answer, .jinja2
and .j2
are trending.
更新:自从我写下这个答案以来,事情发生了变化,.jinja2
并且.j2
是趋势。
回答by elemel21
I use .jnj extension - and for syntax highlighting and for snippets in vim I just copied, renamed and tweaked settings for twig.
我用.jnj扩展-和语法高亮和vim的片断我只是复制,重命名和调整设置树枝。
When I need some settings for html (like commenting, tag balancing, indenting etc) in jinja template, I also have a function set a time ago to work with PHP and WordPress - it toggles to html and back to previous filetype:
当我需要在 jinja 模板中对 html 进行一些设置(如评论、标签平衡、缩进等)时,我还设置了一个函数来使用 PHP 和 WordPress - 它切换到 html 并返回到以前的文件类型:
let g:my_file_type = 0
function! ToggleFileType()
if g:my_file_type == 0
set ft=html
let g:my_file_type = 1
else
filetype detect
let g:my_file_type = 0
endif
endfunction
And it is bound to F12 key with nmap <silent> <F12> :call ToggleFileType()<CR>
并且它绑定到 F12 键 nmap <silent> <F12> :call ToggleFileType()<CR>
回答by Johnride
Ansible uses the .j2
extension.
Ansible 使用.j2
扩展。
I couldn't find a definitive documentation about that precise point but we see occurences of the .j2
extension in many places of their documentation :
我找不到关于那个精确点的权威文档,但我们.j2
在他们文档的许多地方看到了扩展的出现:
If you look for .j2
in the following pages you'll see many occurences :
如果您.j2
在以下页面中查找,您会看到很多情况:
http://docs.ansible.com/ansible/template_module.htmlhttp://docs.ansible.com/ansible/playbooks_intro.html
http://docs.ansible.com/ansible/template_module.html http://docs.ansible.com/ansible/playbooks_intro.html
This is the convention that I use for other projects as well, except django templates.
这也是我用于其他项目的约定,除了 django 模板。
回答by rd2
Just FYI - Johnride above mentioned about Ansible using .j2
as their convention for template file which is correct, just pointing out the "best practices" documented they have now put out, which mentions:
仅供参考 - Johnride 上面提到了 Ansible 使用.j2
作为模板文件的约定,这是正确的,只是指出了他们现在已经发布的“最佳实践” ,其中提到:
templates end in
.j2
模板以
.j2
回答by Kiran Jonnalagadda
I use .html.jinja2
, .js.jinja2
, .css.jinja2
etc to indicate that (a) it's a Jinja2 template, and (b) will compile into a HTML, JS or CSS file. I like the .j2
choice in Ansible, but IMO using .jinja2
makes it easier for a new contributor to guess what templating language is being used.
我用.html.jinja2
,.js.jinja2
,.css.jinja2
等来表示(一)这是一个Jinja2的模板,和(b)将汇编成HTML,JS或CSS文件。我喜欢.j2
Ansible 中的选择,但 IMO 的使用.jinja2
使新贡献者更容易猜测正在使用什么模板语言。
For Flask users, since auto-escaping is nice to have:
对于 Flask 用户,因为自动转义很不错:
def _select_jinja_autoescape(filename):
"""
Returns `True` if autoescaping should be active for the given template name.
"""
if filename is None:
return False
return filename.endswith(('.html', '.htm', '.xml', '.xhtml',
'.html.jinja', '.html.jinja2', '.xml.jinja', '.xml.jinja2',
'.xhtml.jinja', '.xhtml.jinja2'))
app.jinja_env.autoescape = _select_jinja_autoescape
回答by Migwell
回答by geerlingguy
I wanted to add an additional answer in 2020, as recently the Jinja2 project decided to drop the '2' from the name... meaning it's now just "Jinja".
我想在 2020 年添加一个额外的答案,因为最近 Jinja2 项目决定从名称中删除“2”......意味着它现在只是“Jinja”。
Ansible still uses .j2
in a lot of documentation, but seeing as the official Jinja template engine documentation now recommends .jinja
if you want to use something other than non-Jinja-specific file extensions (see docs here, or when the change was added), I think people will start moving that way (and dropping the use of .j2
or .jinja2
).
Ansible 仍然.j2
在很多文档中使用,但是现在看到官方 Jinja 模板引擎文档建议.jinja
如果您想使用非 Jinja 特定文件扩展名以外的其他内容(请参阅此处的文档,或添加更改时),我认为人们将开始以这种方式移动(并放弃使用.j2
或.jinja2
)。