Search code examples
pythondjangodjango-templatesdjango-1.10

Combine tags with filters


Code:

{% firstof m.caption m.altcaption|slice:":37" %}

I want to choose the first of those 2 variables, then slide to 37 characters. Any ideas?


Solution

  • In Django 1.9+ you can assign the result of the firstof tag to a variable.

    {% firstof m.caption m.altcaption as caption %}
    {{ caption|slice:":37"}} 
    

    In earlier versions you could filter both arguments

    {% firstof m.caption|slice:":37" m.altcaption|slice:":37" %}