When I investigated what a template engine that can be written like twig with python, jinja seemed to be close to it, so I put it in a memo.
Add jinja2 to requires.
setup.py
#Add the following
requires = [
'pyramid_jinja2',
]
Added to include under app.main. And added jinja2.directories.
development.ini
[app:main]
pyramid.include =
pyramid_debugtoolbar
pyramid_tm
pyramid_jinja2
#Myapp is the application name
jinja2.directories = myapp:templates
Do the following to get jinja2
python setup.py develop
Add various things to config.
__init__.py
def main(global_config, **settings):
# config.scan()Added before
config.include('pyramid_jinja2')
config.add_renderer(".html", "pyramid_jinja2.renderer_factory")
#myapp is the application name
config.add_jinja2_search_path("myapp:templates")
config.scan()
With this, if you call templates with the default extension of pt as html, you can write like twig!
Recommended Posts