Login

Template filter to turn Twitter names into links

Author:
mrben_
Posted:
July 14, 2010
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

Quick and simple twitterize filter to turn Twitter usernames into profile links on your own sites.

Add the filter code to your own template tags (http://docs.djangoproject.com/en/dev/howto/custom-template-tags/).

 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. New Snippet! by Antoliny0919 4 days, 16 hours ago
  2. Add Toggle Switch Widget to Django Forms by OgliariNatan 2 months, 3 weeks ago
  3. get_object_or_none by azwdevops 6 months, 2 weeks ago
  4. Mask sensitive data from logger by agusmakmun 8 months, 1 week ago
  5. Template tag - list punctuation for a list of items by shapiromatron 1 year, 10 months ago

Comments

Please login first before commenting.