You are pretty close, you just need to move it to your Python script. Thus, you can define a predicate as follows:
def short_caption(someitem): return len(someitem) < 60
Then register it in the environment by adding it to the "test" dict):
your_environment.tests["short_caption"] = short_caption
And you can use it as follows:
{% if item[0] is short_caption %} {# do something here #} {% endif %}
For more information, here are jinja docs on user tests
(you only need to do this once, and I think that it is important whether you do this before or after you start creating templates, the documents are unclear)
If you are not already using the environment, you can create it as follows:
import jinja2 environment = jinja2.Environment()
And then load your template using the get_string () method:
template = environment.from_string("""your template text here""") template.render(items=links).encode('utf-8')
Finally, as a side note, if you use a file loader, it allows you to inherit files, import macros, etc. Basically, you just save your file as it is now and tell jinja where the directory is.
Jeff tratner
source share