tread
tread

Reputation: 11098

How to neaten a long built in template tag in django?

How do you neaten a long template tag like this:

<td>{{ membership.is_project_manager|yesno:"<span class='glyphicon glyphicon-ok'></span>,<span class='glyphicon glyphicon-remove'></span>"|safe  }}</td>

Upvotes: 0

Views: 27

Answers (1)

Alasdair
Alasdair

Reputation: 308899

You are duplicating a lot of code in the yesno tag. All you really need is glyphicon-ok or glyphicon-remove. The rest is displayed all the time.

You could change it to:

<td><span class='glyphicon {{ membership.is_project_manager|yesno:"glyphicon-ok,glyphicon-remove" }}'></span></td>

You could also move the glyphicon- part out of the yesno tag if you want.

Personally I might find a simple if tag more readable, but that's a personal preference.

<td><span class='glyphicon {% if membership.is_project_manager %}glyphicon-ok{% else %}glyphicon-remove{% endif %}'></span></td>

If you found yourself writing this code a lot then you could write a custom template tag that returns glyphicon-ok or glyphicon-remove. That could simplify the template down to something like:

<td><span class='glyphicon {% glyphicon_class membership %}'></span></td>

Upvotes: 3

Related Questions