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?
POST form (your current approach) This answer is perfect and I learned a great deal!
extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.
What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.
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'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With