How to make a python package containing only jinja templates

不羁的心 提交于 2019-12-04 05:21:36

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/29150156/how-to-make-a-python-package-containing-only-jinja-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!