Opened 16 years ago
Closed 16 years ago
#13379 closed (wontfix)
New template filter suggestion (wrap)
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Template system | Version: | dev |
| Severity: | Keywords: | wrap, template filter | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
We came across a use case today for wrapping variables in styles If they are not None/Empty/False...
E.g. If a variable exists add a break tag after it or if a value exists then wrap it in a <p></p> tag.
So we wrote a simple template tag that might be of interest to the community.
Template tag:
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter
@stringfilter
def wrap(value, arg):
if value and value != '':
return mark_safe(arg % value) # decision needed with |safe
return value
Usage:
{{ var|wrap:"%s<br />" }} or
{{ var|wrap:"<p>%s</p>" }}
Change History (1)
comment:1 by , 16 years ago
| milestone: | 1.2 |
|---|---|
| Resolution: | → wontfix |
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
I'm afraid I don't see the benefit here. What you describe is already possible with:
{% if var %}<p>{{ var }}</p>{% endif %}I'm not convinced that a template filter is an inhernetly cleaner way of representing this.