Using Custom Tag for embedding the Video in Grails

14 / Dec / 2010 by Anuj Aneja 1 comments

I had to make a video available on my GSP page. So I looked at the grails flash-player plugin.  After going through the documentation I found that it could not be used with videos on YouTube.

This code given below will generate the script for the flash player on your GSP for playing youtube videos.

<object width="480" height="385">
<param name="movie" value="http://www.youtube-nocookie.com/v/YG9RGKYauhE?fs=1&amp;amp;hl=en_US">
</param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always">
</param>
<embed src="http://www.youtube-nocookie.com/v/YG9RGKYauhE?fs=1&amp;amp;hl=en_US"
type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480"
height="385">
</embed></object>

This is the general code for a YouTube video. URL in the browser for video looks like -http://www.youtube.com/watch?v=YG9RGKYauhE&feature=related.

The common thing between embed code and URL of video is the key for the video which uniquely identifies it. So copy and paste the code in your GSP page and pass the video key to change the video as you want.

But to make it simple I made a custom tag like–

def video={attrs->
        def videoKey=attrs['videoKey']
         def vd={
           object(width:attrs['width']?:"640",height:attrs['height']?:"385"){
             param(name:"movie",value:"http://www.youtube-nocookie.com/v/${videoKey}?fs=1&amp;amp;hl=en_US")
             param(name:"allowFullScreen",value:"true")
             param(name:"allowscriptaccess",value:"always")
             embed(src:"http://www.youtube-nocookie.com/v/${videoKey}?fs=1&amp;amp;hl=en_US",
type:"application/x-shockwave-flash",allowscriptaccess:"always",allowfullscreen:"true",
width:"${attrs['width']?:'640'}", height:"${attrs['height']?:'385'}")

           }

         }
         def xml=new groovy.xml.StreamingMarkupBuilder().bind(vd)
         out<<xml
}

so you just need to write the following tag which will set its default height, width etc.

<my:image videoKey=”-------the key from youtube---------” />

I hope this helps!

FOUND THIS USEFUL? SHARE IT

comments (1 “Using Custom Tag for embedding the Video in Grails”)

Leave a comment -