Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • MartinB 411 posts 512 karma points
    Aug 08, 2012 @ 22:40
    MartinB
    0

    Fixed: Youtube embed through XSLT

    Just spent the last hour to figure out why my "embed-youtube" xslt was breaking my page layout (hiding all HTML that came after the video embed code).

    Then finally it struck me that it was because there was no data between the iframe start and end tag and xslt does not seem to like that :/

    <iframe width="100" height="100"> 'empty space' </iframe>

    So with a little work around i got i working:

    <xsl:param name="currentPage"/>
    <xsl:variable name="video" select="$currentPage/frontpageMedia"/>
    
    <xsl:template match="/">
    
    <!-- start writing XSLT -->
    <xsl:if test="$video != '' ">     
        <xsl:element name="iframe">
            <xsl:attribute name="class">cf</xsl:attribute>  
            <xsl:attribute name="width">440</xsl:attribute>
            <xsl:attribute name="height">260</xsl:attribute>
            <xsl:attribute name="src">http://www.youtube.com/embed/<xsl:value-of select="$video"/></xsl:attribute>
            <xsl:attribute name="frameborder">0</xsl:attribute>
            <xsl:comment/><!-- avoid empty tag value that breaks the html-->
        </xsl:element>
    </xsl:if>
    
    </xsl:template>

    $currentPage/frontpageMedia is a Singleline text field where the key for the youtube video is pasted into.

    I.E: PbK9eybE35E

    Hope it can help someone in a similar situation.

    Have a nice day :-)

  • MartinB 411 posts 512 karma points
    Aug 08, 2012 @ 22:43
    MartinB
    0

    Umbraco version 4.7.2

  • Bjarne Fyrstenborg 1160 posts 3356 karma points MVP 4x c-trib
    Aug 09, 2012 @ 08:43
    Bjarne Fyrstenborg
    1

    Yes, you have to make sure, that the data in the container not is empty, otherwise the html unfortunately is self-closing and it might break the layout..

    There are multiple ways to fix this.. e.g as you mention with inserting a <xsl:comment /> or you can force an empty space <xsl:text> </xsl:text> ... or you can change output to html: 

    <xsl:outputmethod="html"omit-xml-declaration="yes"/>

    http://our.umbraco.org/wiki/how-tos/xslt-useful-tips-and-snippets/stop-html-tags-from-self-closing

    /Bjarne

  • Suvir 1 post 20 karma points
    Nov 25, 2016 @ 09:35
    Suvir
    0

    Thanks for the xsl positing you did with youtube example, it was pretty useful. Thanks a ton :)

Please Sign in or register to post replies

Write your reply to:

Draft