Arbazz Hussain
Arbazz Hussain

Reputation: 1932

Django Template how to apply filter on the result returned by simple tag

I have a following simple_tag.

@register.simple_tag
def Test(arg1,arg2)
   return arg1+arg2

And in the template.

 <h6>{% Test val.arg1 val.arg2 %}</h6>

And now I want to apply the filter on the above returned data from simple_tag Test,

for example, I want to apply naturaltime filter on the returned data, how to do it along with the simple tag.

<h6>{% Test val.arg1 val.arg2 | naturaltime %}</h6>

Upvotes: 2

Views: 383

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477200

For a simple tag, you can store the result of a template tag in a variable with the as keyword, as is specified in the documentation on simple tags:

It’s possible to store the tag results in a template variable rather than directly outputting it. This is done by using the as argument followed by the variable name. Doing so enables you to output the content yourself where you see fit:

{% current_time "%Y-%m-%d %I:%M %p" as the_time %}
<p>The time is {{ the_time }}.</p>

So in this case we can render the output with:

<h6>{% Test val.arg1 val.arg2 as result %}{{ result|naturaltime }}</h6>

Upvotes: 5

Related Questions