Template filter to turn Twitter names into links

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# template tags file
import re
from django import template
register = template.Library()

@register.filter(name='twitterize')
def twitterize(token):
    return re.sub(r'\W(@(\w+))', r'<a href="https://twitter.com/\2">\1</a>', token)
twitterize.is_safe = True

# Use in your templates like this:
<ul>
  {% for t in tweets %}
    <li>{{ t.text|urlize|twitterize }}</li>
  {% endfor %}
</ul>

More like this

  1. Template range filter by zalun 4 years, 2 months ago
  2. isoutc template filter by japerk 4 years, 1 month ago
  3. Template filter to convert timecodes into links by justin_h 3 years, 8 months ago
  4. "Partial Templates" - an alternative to "include" by vigrid 4 years, 3 months ago
  5. Template tag to clear cached template fragment by joao.coelho 3 years, 6 months ago

Comments

(Forgotten your password?)