How to make a python package containing only jinja

2019-03-01 03:12发布

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条回答
做个烂人
2楼-- · 2019-03-01 03:37

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

查看更多
登录 后发表回答