Created
August 4, 2012 06:33
-
-
Save JonCooperWorks/3255152 to your computer and use it in GitHub Desktop.
Jinja2 Filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@evalcontextfilter | |
def to_phone_number(eval_ctx, value): | |
#Start by ensuring string has 10 digits | |
digits = filter(lambda x: x.isdigit(),value) | |
if len(digits) == 10: | |
phone_number = '(%s)%s-%s'%(digits[0:3], digits[3:6], digits[6:]) | |
else: | |
phone_number = '%s-%s'%(digits[0:3], digits[3:]) | |
if eval_ctx.autoescape: | |
return Markup(phone_number) | |
return phone_number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment