Login

"Youtube watch link to embed" custom tag.

Author:
I159
Posted:
September 25, 2011
Language:
Python
Version:
1.3
Tags:
youtube embed custom-tag
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. youtubize template tag by TheJester 8 years, 2 months ago
  2. YouTube Template Filter by jgeewax 7 years ago
  3. Template filter to convert timecodes into links by justin_h 5 years, 10 months ago
  4. ultralize by catellar 3 years, 10 months ago
  5. "Link To" helper tag by jdriscoll 7 years, 9 months ago

Comments

Please login first before commenting.