Last active
April 15, 2019 05:38
-
-
Save joncombe/a208be990d7b98c15b8dbf5475eb75c3 to your computer and use it in GitHub Desktop.
Django template tag to format Thai phone numbers
This file contains hidden or 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
""" | |
# https://gist.github.com/joncombe/a208be990d7b98c15b8dbf5475eb75c3 | |
{% load thai_phone %} | |
{{ landline|thai_phone }} # 02 123 4567 | |
{{ mobile|thai_phone }} # 081 234 5678 | |
{{ mobile|thai_phone:'-' }} # 081-234-5678 | |
""" | |
from django import template | |
register = template.Library() | |
@register.filter(is_safe=True) | |
def thai_phone(phone, separator=' '): | |
if len(phone) == 10: | |
return '%s%s%s%s%s' % (phone[0:3], separator, phone[3:6], separator, phone[6:10]) | |
if len(phone) == 9: | |
return '%s%s%s%s%s' % (phone[0:2], separator, phone[2:5], separator, phone[5:9]) | |
return phone |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment