Search code examples
pythondjangodjango-cms

How to get property from object returned by a custom templatetag filter without another filter


I'm usign Django-CMS (v3.4.6), Django (v1.11), and Python (v2.7.x).

I have a custom templatetag filter called get_foo that takes an my_obj, does some processing and then returns an instance known to have property bar, however when I attempt to do the following, I get an TemplateSyntaxError: Could not parse some characters: my_obj|get_foo|.bar error: {% my_obj|get_foo.bar %}

The only workaround I've found is creating another filter which returns bar and so the line in my template looks like this: {% my_obj|get_foo|get_bar %}. But I want to be sure: isn't there a way to do this without creating yet another filter?


Solution

  • You can use the with tag:

    {% with my_obj|get_foo as my_foo %}
        {{ my_foo.bar }}
    {% endwith %}