Method similar to 'startswith' in Jinja2/Flask

A better solution....

You can use startswith directly in field because field is a python String.

{% if field.startswith('i-') %}

More, you can use any String function, including str.endswith(), for example.


The expression {% if {{ field | startswith }} %} will not work because you cannot nest blocks inside each other. You can probably get away with {% if (field|startswith) %} but a custom test rather than a filter, would be a better solution.

Something like

def is_link_field(field):
    return field.startswith("i-"):

environment.tests['link_field'] = is_link_field

Then in your template, you can write {% if field is link_field %}

Tags:

Jinja2

Flask