Support for Jinja2 HTML templates (for a Flask template)
See original GitHub issueI am in the process of making a Flask cookiecutter template, but I’m running into problems when the Flask Jinja templates are added.
As an example, say I have this template:
<p>Go <a href="{{ url_for('home') }}">Home</a></p>
Then when I run cookiecutter cookiecutter-flask
, I get a jinja2.exceptions.UndefinedError
:
Traceback (most recent call last):
File "/Users/sloria1/Envs/cookiecutter-flask/bin/cookiecutter", line 8, in <module>
load_entry_point('cookiecutter==0.6.1', 'console_scripts', 'cookiecutter')()
File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/cookiecutter/main.py", line 90, in main
cookiecutter(args.input_dir)
File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/cookiecutter/main.py", line 61, in cookiecutter
context=context
File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/cookiecutter/generate.py", line 88, in generate_files
rendered_file = tmpl.render(**context)
File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "./cookiecutter-pypackage/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/home.html", line 1, in top-level template code
<p>Go <a href="{{ url_for('home') }}">Home</a></p>
jinja2.exceptions.UndefinedError: 'url_for' is undefined
Issue Analytics
- State:
- Created 10 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Rendering Pages in Flask Using Jinja - Hackers and Slackers
We'll start by creating an app that uses 1 of each of the three template types: layout.html, home.html, and nav.html. These templates will...
Read more >HTML Templates with Jinja - Flask Tutorial - techwithtim.net
This flask tutorial will show you how to use the HTML template engine called jinja. It allows you to write dynamic HTML code...
Read more >Templates — Flask Documentation (2.2.x)
This section only gives a very quick introduction into how Jinja2 is integrated into Flask. If you want information on the template engine's...
Read more >Learn Flask: Jinja2 Templates and Forms Cheatsheet
Templates are enabled using the Jinja2 template engine and allow data to be ... When it comes to HTML web forms, the Flask...
Read more >Primer on Jinja Templating - Real Python
With Jinja, you can build rich templates that power the front end of ... Create a new template named base.html in your templates/...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
I had a similar problem with creating a Django cookiecutter template (https://github.com/pydanny/cookiecutter-dj-package) that I need to go fix. 😉
I think escaping (http://jinja.pocoo.org/docs/templates/#escaping) is the answer. Try:
If that doesn’t work, try:
You can also escape just the
{{
like this:Same for escaping
}}
with{{ "}}" }}
.I do something similar to escape
{
here: https://github.com/audreyr/cookiecutter-jquery/blob/master/{{cookiecutter.repo_name}}/src/jquery.{{cookiecutter.repo_name}}.coffee#L40The downside is that Jinja-templated templates are not the prettiest to read. (Any ideas how to improve this, anyone?)
I will add this to the documentation when I get a chance, since it’s a common scenario.