简体   繁体   中英

Django template passing a template variable into cut filter

I am trying to pass a template into a cut filter, something like this

{{ myVariable|cut:"something + templateVariable" }}

I've tried:

{{ myVariable|cut:"something"|add:templateVariable }}

and

{{ myVariable|cut:"something {{ templateVariable }}" }}

but these does not work.

Is this possible to do?

It should work with a temporary variable using the with tag :

{% with myFilter="something"|add:templateVariable %}
    {{ myVariable|cut:myFilter }}
{% endwith %}

Or in Django 1.2 and older:

{% with "something"|add:templateVariable as myFilter %}
    {{ myVariable|cut:myFilter }}
{% endwith %}

Add does not support concatenation of string and int but you could easily make a template filter that converts to string for example:

from django import template

register = template.Library()

@register.filter
def to_unicode(mixed):
    return unicode(mixed)

Would allow a such template tag expression some_int|to_unicode|add:'foo' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM