如何制作一个只包含jinja模板的python包 [英] How to make a python package containing only jinja templates

查看:143
本文介绍了如何制作一个只包含jinja模板的python包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前有一个项目,我目前正在试图扩展jinja2模板,生活在我正在做的python包。现在我正努力使用 .html 文件创建一个python包。这是我现在有的:

  sharedtemplates / 
├──setup.py
└──模板
├──__init__.py
├──base.html
├──footer.html
└──header.html

__ init __。py 为空, setup.py <

目前我正在处理的目录是这样设置的:

  repo / 
├──site.py
└──模板
└──index.html

index.html 我会有 {%extends'base .html'%} sharedtemplates 包中扩展基础。



site.py 有这个优先模板加载的优先次序:

  template_loader = jinja2.ChoiceLoader([
jinja2.PackageLoader('reposhared','templates'),
app.jinja_loader
])

app.jinja_loader = template_loader

目录共享模板/首先模板/在我目前的回购目录。



谢谢。 我忘了做python setup。 py安装。我需要将模板放在另一个模板目录中。所以它是共享模板/模板/模板/ base.html。绝对需要做一些重命名和重构


Currently have a project where I am currently trying to extend jinja2 templates that live in a python package I am trying to make. Right now I'm struggling to make a python package with .html files. Here is what I currently have:

sharedtemplates/
├── setup.py
└── templates
    ├── __init__.py
    ├── base.html
    ├── footer.html
    └── header.html

__init__.py is empty and setup.py is super basic.

The directory I am currently working on is setup like this:

repo/
├── site.py
└── templates
    └── index.html

In index.html I would have {% extends 'base.html' %} to extend base in the sharedtemplates package.

site.py has this in there to prioritize the template loading:

template_loader = jinja2.ChoiceLoader([
    jinja2.PackageLoader('reposhared', 'templates'),
    app.jinja_loader
])

app.jinja_loader = template_loader

So this would load the templates dir in sharedtemplates/ first the templates/ in my current repo dir.

Thank you.

解决方案

I forgot to do python setup.py install. And I needed to throw the templates in another templates dir. So it is sharedtemplates/templates/templates/base.html. Definitely need to do some renaming and refactoring

这篇关于如何制作一个只包含jinja模板的python包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆