Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Jinja2 HTML templates (for a Flask template) #9

Closed
sloria opened this issue Aug 14, 2013 · 5 comments
Closed

Support for Jinja2 HTML templates (for a Flask template) #9

sloria opened this issue Aug 14, 2013 · 5 comments
Labels
needs-docs PR Only: This PR require additional documentation

Comments

@sloria
Copy link
Contributor

sloria commented Aug 14, 2013

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
@pydanny
Copy link
Member

pydanny commented Aug 14, 2013

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:

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

If that doesn't work, try:

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

@audreyfeldroy
Copy link
Member

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.

@sloria
Copy link
Contributor Author

sloria commented Aug 16, 2013

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

@pydanny
Copy link
Member

pydanny commented Aug 24, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-docs PR Only: This PR require additional documentation
Projects
None yet
Development

No branches or pull requests

3 participants