简体   繁体   English

Django模板将模板变量传递到剪切过滤器

[英]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: 或者在Django 1.2及更早版本中:

{% 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: 添加不支持字符串和int的连接,但您可以轻松地创建一个转换为字符串的模板过滤器 ,例如:

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' . 允许这样的模板标记表达式some_int|to_unicode|add:'foo'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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