Search code examples
djangodjango-templatesdjango-tagging

Filter in custom Django tag?


I created a custom tag, that accepts a parameter:

{% panel "most" %}

I would use the add to the parameter:

{% panel "most"|add:"_people" %}

I expect the parameter will be "most_people", but instead it is "most"|add:"_people". It seems that filters are not invoked inside custom tags. How to make them work?


Solution

  • If you use the simple_tag decorator to write your custom tag, you should be able to use a filters on the arguments.

    However, if you have written an advanced template tag, then you will need to do extra work to make "most"|add:"_people" use the filter as you want.

    One work around, is to use the {% with %} tag to assign the result of the filter to a variable.

    {% with panel_name="most"|add:"_people" %}
    {% panel panel_name %}
    {% endwith %}