Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template filter to trim any leading or trailing whitespace

Is there a template filter in django that will trim any leading or trailing whitespace from the input text.

Something like: {{ var.example|trim }}

like image 639
John Avatar asked Apr 28 '12 06:04

John


2 Answers

You can do it yourself

from django import template from django.template.defaultfilters import stringfilter  register = template.Library()  @register.filter @stringfilter def trim(value):     return value.strip() 

Documentation

like image 31
San4ez Avatar answered Sep 20 '22 17:09

San4ez


Django templates allow you to access methods and properties by using the '.' syntax:

{{ var.example.strip }} 

You can extend this by chaining other filters when you're dealing with HTML, e.g.:

{{ var.example.strip|safe|removetags:"p img" }} 

Here we first remove any <p> and <img> tags, then tell Django it can safely render the rest of the content, which we have stripped of any whitespace.

like image 147
Lukas Batteau Avatar answered Sep 22 '22 17:09

Lukas Batteau