Cookiecutter: Support for Jinja2 HTML templates (for a Flask template)

Created on 14 Aug 2013  路  5Comments  路  Source: cookiecutter/cookiecutter

I 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
needs-docs

Most helpful comment

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/%7B%7Bcookiecutter.repo_name%7D%7D/src/jquery.%7B%7Bcookiecutter.repo_name%7D%7D.coffee#L40

The 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.

All 5 comments

I had a similar problem with creating a Django cookiecutter template (https://github.com/pydanny/cookiecutter-dj-package) that I need to go fix. :wink:

I think escaping (http://jinja.pocoo.org/docs/templates/#escaping) is the answer. Try:

{{ {{ url_for('home') }} }}

If that doesn't work, try:

{% raw %}
<p>Go <a href="{{ url_for('home') }}">Home</a></p>
{% endraw %}

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/%7B%7Bcookiecutter.repo_name%7D%7D/src/jquery.%7B%7Bcookiecutter.repo_name%7D%7D.coffee#L40

The 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.

OK, using the {% raw %} tag in all my templates worked well. Thanks for your help.

@audreyr - I think this is resolved. Can you close please?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

audreyfeldroy picture audreyfeldroy  路  6Comments

milost picture milost  路  5Comments

hackebrot picture hackebrot  路  5Comments

ionelmc picture ionelmc  路  6Comments

khorn picture khorn  路  10Comments