Name Capitalize Filter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@register.filter
def cap(value):
    namelist = value.split(' ')
    fixed = ''
    for name in namelist:
        name = name.lower()
        # fixes mcdunnough
        if name.startswith('mc'):
            sub = name.split('mc')
            name = "Mc" + sub[1].capitalize()
        # fixes "o'neill"
        elif name.startswith('o\''): 
            sub = name.split('o\'')
            name = "O'" + sub[1].capitalize()

        else: name = name.capitalize()
        
        nlist = name.split('-')
        for n in nlist:
            if len(n) > 1:
                up = n[0].upper()
                old = "-%s" % (n[0],)
                new = "-%s" % (up,)
                name = name.replace(old,new)

        fixed = fixed + " " + name
    return fixed

More like this

  1. Convert numbers in USA notation to brazilian notation by eOliva 5 years, 6 months ago
  2. Django 1.2+ template loader for Jinja2 by SimonSapin 3 years, 10 months ago
  3. Convert LaTeX templates to various output formats by blizz 7 years, 1 month ago
  4. Template filters utils by marinho 6 years, 5 months ago
  5. Template filter to convert timecodes into links by justin_h 4 years, 7 months ago

Comments

onlinehero (on July 13, 2009):

Thanks, this is much more useful than Pythons own capitalization methods!

#

(Forgotten your password?)