Login

"Youtube watch link to embed" custom tag.

Author:
I159
Posted:
September 25, 2011
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

Tag for simple embed video from youtube watch link. It can be modified to manage size of embedded video.

 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
28
29
30
from django import template
import re

register = template.Library()

class YoutubeNode(template.Node):
    """Convenient tag, allowing only having a normal watch link, from yotube, 
    embed video in html."""
    
    def __init__(self, parsed_link):
        self.parsed_link = parsed_link
            
    def render(self, context):
        del_it = re.compile('(&amp.*)')
        replace_it = re.compile('watch\?v=')
        link = self.parsed_link.render(context)
        link = del_it.sub('', link)
        link = replace_it.sub('embed/', link)
        video = """<iframe width="540" height="432" src="%s" 
        frameborder="0" allowfullscreen></iframe>""" % link
        return video

@register.tag
def youtube(parser, token):    
    parsed_link = parser.parse(('endyoutube',))
    # first token it's closing tag. delete_first_token 
    # just delete it - del self.tokens[0];)
    print type(parsed_link)
    parser.delete_first_token()
    return YoutubeNode(parsed_link)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 12 months ago

Comments

Please login first before commenting.