jinja2

How do you debug Mako templates?

So far I've found it impossible to produce usable tracebacks when Mako templates aren't coded correctly. Is there any way to debug templates besides iterating for every line of code? Edit: Per the comments, it seems switching to Jinja2 might be the best solution. ...

Any drawbacks or gotchas to using Jinja2 templates in Django?

After reading the Jinja2 documentation, I'm interested in employing it in future Django projects. However, I'm wondering if anyone has encountered any drawbacks or gotchas when using Jinja2 templates with Django? If so, how did you work around them? I wouldn't mind hearing about positive experiences either, just to get a good cross se...

How can I create a tag in Jinja that contains values from later in the template?

I'm using Jinja2, and I'm trying to create a couple tags that work together, such that if I have a template that looks something like this: {{ my_summary() }} ... arbitrary HTML ... {{ my_values('Tom', 'Dick', 'Harry') }} ... arbitrary HTML ... {{ my_values('Fred', 'Barney') }} I'd end up with the following: This page includes inform...

Context processor using Werkzeug and Jinja2

My application is running on App Engine and is implemented using Werkzeug and Jinja2. I'd like to have something functionally equivalent of Django's own context processor: a callable that takes a request and adds something to the template context. I already have a "context processors" that add something to the template context, but how d...

Optimizing Jinja2 Environment creation

My application is running on Google App Engine and most of requests constantly gets yellow flag due to high CPU usage. Using profiler I tracked the issue down to the routine of creating jinja2.Environment instance. I'm creating the instance at module level: from jinja2 import Environment, FileSystemLoader jinja_env = Environment(loader...

Python: Running all unit tests inside a package

I'm trying to hack my through an open source python project (namely: jinja2), When I say "I'm hacking my way through", I mean I don't really know what I'm doing, so I want to run unittests whenever I change something to make sure I'm not breaking something major! There's a package full of unit tests (if you want to have a look, it's h...

Multiple blocks of same name in Jinja2

In Jinja2, I have a base template like this: <title>{% block title %}{% endblock %} - example.com</title> [...] <h1> {% block title %}{% endblock %} - example.com </h1> Jinja2, then, fails with the following message: lines = [self.message, ' ' + location] : block 'title' defined twice It must be now evident as to what I am t...

How do you mark strings as "Safe" in a view (ot the template) in Jinja2?

Typically when you want to mark string output as safe in Jinja2 you do something like this: {{ output_string|safe() }} However, what if output_string is always safe? I don't want to repeat myself every time by using the safe filter. I have a custom filter called "emailize" that preps urls for output in an email. The ampersands always...

What is the fastest template system for Python?

Jinja2 and Mako are both apparently pretty fast. How do these compare to (the less featured but probably good enough for what I'm doing) string.Template ? ...

Jinja2 If Statement

The code below is a sample form I'm using to learn jinja2. As written, it returns an error saying that it doesn't recognize the {% endif %} tag. Why does this happen? <html> Name: {{ name }} Print {{ num }} times Color: {{ color }} {% if convert_to_upper %}Case: Upper {% elif not convert_to_upper %}Case: Lower{% endif %} {% for r...

vim syntax highlighting for jinja2?

How do you do jinja2 aware syntax highlighting for vim? ...

jinja2: get lengths of list

How do I get the number of elements in a list in jinja2 template. For example, in python: print template.render(products=[???]) and in jinja2 <span>You have {{what goes here?}} products</span> ...

How create jinja2 extension?

I try to make extension for jinja2. I has written such code: http://dumpz.org/12996/ But I receive exception: "'NoneType' object is not iterable" Where is a bug? That should return parse. Also what should accept and return _media? ...

jinja2: html escape variables

how do I html-escape dangerous unsanitized input in jinja2? Can I do it inside the template or must it be done in python code? I have a variable that may contain da<ngero>u&s chars. How do I escape it in jinja2 ...

How does the Jinja2 "recursive" tag actually work?

I'm trying to write a very simple, tree-walking template in jinja2, using some custom objects with overloaded special methods (getattr, getitem, etc) It seems straightforward, and the equivalent python walk of the tree works fine, but there's something about the way that Jinja's recursion works that I don't understand. The code is show...

jinja2: get loop index of outer loop

In jinja, the variable loop.index holds the iteration number of the current running loop. When I have nested loops, how can I get in the inner loop the current iteration of an outer loop? ...

How to get django context automatically in Jinja2 filters?

For example, I have an paginator object with a lot of attributes, and don't want do write something like {{ paginate(paginator) }} in templates. How can a get context automatically in the filter function, like a django register.inclusion_tag(…, takes_context=True)? Yes, of course, I can do something like paginate(paginator), but it loo...

How can I access response.context when testing a Jinja2 powered Django view

When I use the Django test.client and I do something like: class MyTestCase(TestCase): def test_this(self): c = self.client response = c.get('/') assert False, response.context['name'] I get an error: assert False, response.context['name'] TypeError: 'NoneType' object is unsubscriptable My only guess is ...

How do you sort a list in Jinja2?

I am trying to do this: {% for movie in movie_list | sort(movie.rating) %} But that's not right...the documentation is vague...how do you do this in Jinja2? ...

Multiple level template inheritance in Jinja2?

I do html/css by trade, and I have been working on and off django projects as a template designer. I'm currently working on a site that uses Jinja2, which I have been using for about 2 weeks. I just found out through reading the documentation that Jinja2 doesn't support multiple level template inheritance, as in you can't do more than on...