Context
I'm trying to use the django template engine to render translatable part of a page using variable from the context. Specifically, this is my use case
{% blocktrans trimmed with type=training.type title=training.title city=training.city %}
Training {{ type }} "{{ title }}" at {{ city }}
{% endblocktrans %}
This works fine, however, my issue with it that the blocktrans
length grows very quicky above the acceptable (at least, for me).
As an alternative, I could do
{% with training.title as title %}
{% with training.type as type %}
{% with training.city as city %}
{% blocktrans trimmed %}
Training {{ type }} "{{ title }}" at {{ city }}
{% endblocktrans %}
{% endwith %}
{% endwith %}
{% endwith %}
Or using one separate with clause.
But that does not really ease the readability of that code, especially if there is more variables to expose to the translation string.
Solution I thought of
My initial idea was to extends the blocktrans tag onto several lines :
{% blocktrans trimmed with
type=training.type
title=training.title
city=training.city %}
Training {{ type }} "{{ title }}" at {{ city }}
{% endblocktrans %}
Unfortunately for me, django does not support multilline tags in templates.
(see the rather lengthy dicussion on django mailing list for the why).
Question
Since I did not find in the above mentioned discussion a solution, I'm searching for a way to obtain the same result as in my first code, without hampering my templates readability with overly long lines/big blocks of with
tags.
I thought about putting that code elsewhere, like a @property
methods in the model training
is based on. But it seems to me that the that logic belongs to the template, (it's very unlikely it will be used anywhere else).
Is there a alternative, idiomatic way to achieve this in django ?