Jinja模板是否有惯用的文件扩展名?

时间:2023-01-13 18:10:50

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内容。

5 个解决方案

#1


9  

Jinja Authors did not define a default extension. Most of Jinja template editors like Vim extension, TextMate extension, Emacs extension, and PyCharm mention no default extension to enforce Jinja highlighting.

Jinja作者没有定义默认扩展名。大多数Jinja模板编辑器,如Vim扩展,TextMate扩展,Emacs扩展和PyCharm都没有提到默认扩展来强制执行Jinja突出显示。

Django had already a similar debate about setting a default extension, and ended as a wontfix issue. I quote from the closing message:

Django已经就设置默认扩展名进行了类似的辩论,并以wontfix问题结束。我引用结束语:

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.

我建议您应该使用自己的,因为没有常见的。

#2


15  

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.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模板之外,这也是我用于其他项目的约定。

#3


6  

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使用.j2作为模板文件的约定是正确的,只是指出了他们现在提出的“最佳实践”,其中提到:

templates end in .j2

模板以.j2结尾

#4


2  

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等来表示(a)它是一个Jinja2模板,(b)将编译成HTML,JS或CSS文件。我喜欢Ansible中的.j2选项,但使用.jinja2的IMO让新贡献者更容易猜出正在使用的模板语言。

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

#5


1  

I use .jnj extension - and for syntax highlighting and for snippets in vim I just copied, renamed and tweaked settings for twig.

我使用.jnj扩展名 - 并且对于语法高亮显示和vim中的片段我只是复制,重命名和调整了twig的设置。

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>

它与nmap 绑定到F12键:调用ToggleFileType()

#1


9  

Jinja Authors did not define a default extension. Most of Jinja template editors like Vim extension, TextMate extension, Emacs extension, and PyCharm mention no default extension to enforce Jinja highlighting.

Jinja作者没有定义默认扩展名。大多数Jinja模板编辑器,如Vim扩展,TextMate扩展,Emacs扩展和PyCharm都没有提到默认扩展来强制执行Jinja突出显示。

Django had already a similar debate about setting a default extension, and ended as a wontfix issue. I quote from the closing message:

Django已经就设置默认扩展名进行了类似的辩论,并以wontfix问题结束。我引用结束语:

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.

我建议您应该使用自己的,因为没有常见的。

#2


15  

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.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模板之外,这也是我用于其他项目的约定。

#3


6  

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使用.j2作为模板文件的约定是正确的,只是指出了他们现在提出的“最佳实践”,其中提到:

templates end in .j2

模板以.j2结尾

#4


2  

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等来表示(a)它是一个Jinja2模板,(b)将编译成HTML,JS或CSS文件。我喜欢Ansible中的.j2选项,但使用.jinja2的IMO让新贡献者更容易猜出正在使用的模板语言。

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

#5


1  

I use .jnj extension - and for syntax highlighting and for snippets in vim I just copied, renamed and tweaked settings for twig.

我使用.jnj扩展名 - 并且对于语法高亮显示和vim中的片段我只是复制,重命名和调整了twig的设置。

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>

它与nmap 绑定到F12键:调用ToggleFileType()