Copied to clipboard

Flag this post as spam?

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


  • stc 72 posts 101 karma points
    May 25, 2010 @ 14:36
    stc
    0

    Template media files

    I'm preety sure this question of mine is one of the first lessons of Umbraco 101 but I've been too long in some other aspects of it that I really can't remeber this...

    I'd like to template umbraco's media files per filetype (file extension). Is it possible (so that every PDF file opens in the same template and not open the file itself - is this the case of passing media url as query param to a template)? Any suggestions please?! Examples would be greatly appreciated.

    Cheers, stc.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    May 26, 2010 @ 12:01
    Lee Kelleher
    2

    Hi stc, do you mean in XSLT? If so, then I use this as an include XSLT:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE xsl:stylesheet [
        <!ENTITY nbsp "&#x00A0;">
    ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="msxml umbraco.library">
        <xsl:output method="xml" omit-xml-declaration="yes" />
    
        <!-- ERROR -->
        <xsl:template match="error" mode="media">
            <xsl:comment>
                <xsl:value-of select="." />
            </xsl:comment>
        </xsl:template>
    
        <xsl:template match="node" mode="media" />
    
        <!-- IMAGE -->
        <xsl:template match="node[@nodeTypeAlias='Image']" mode="media">
            <xsl:if test="count(data) &gt; 0 and string(data[@alias='umbracoFile']) != ''">
                <img src="{data[@alias='umbracoFile']}" height="{data[@alias='umbracoHeight']}" width="{data[@alias='umbracoWidth']}" alt="{@nodeName}" />
            </xsl:if>
        </xsl:template>
    
        <!-- FOLDER -->
        <xsl:template match="node[@nodeTypeAlias='Folder']" mode="media">
            <xsl:if test="count(node) &gt; 0">
                <ul title="{@nodeName}">
                    <xsl:for-each select="node">
                        <li>
                            <xsl:apply-templates select="." mode="media" />
                        </li>
                    </xsl:for-each>
                </ul>
            </xsl:if>
        </xsl:template>
    
        <!-- FILE -->
        <xsl:template match="node[@nodeTypeAlias='File']" mode="media">
            <xsl:if test="count(data) &gt; 0 and string(data[@alias='umbracoFile']) != ''">
                <a href="{data[@alias='umbracoFile']}">
                    <xsl:value-of select="@nodeName" />
                </a>
            </xsl:if>
        </xsl:template>
    
        <!-- PDF -->
        <xsl:template match="node[@nodeTypeAlias='PDF']" mode="media">
            <xsl:if test="count(data) &gt; 0 and string(data[@alias='umbracoFile']) != ''">
                <a href="{data[@alias='umbracoFile']}" class="pdf">
                    <xsl:value-of select="@nodeName" />
                </a>
            </xsl:if>
        </xsl:template>
    
    </xsl:stylesheet>

    Then I can apply the templates using:

    <xsl:apply-templates select="umbraco.library:GetMedia($currentPage/data[@alias='media'], 1)" mode="media" />

    You'll need to tweak the XPath accordingly, but it should work nicely!

    Cheers, Lee.

  • stc 72 posts 101 karma points
    May 26, 2010 @ 17:13
    stc
    0

    Hi Lee,

    Seems that I'm not having a mental block but I'm dumb about XSLT (got any literature to recommend:)...I'm not sure how to use this...I think it should be used somewhat like the RSSFeed XSLT that was written in CWS by Warren but I'm not sure how, since I keep getting errors all the time I try to save it.

    This is what I wrote so far:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE xsl:stylesheet [
            <!ENTITY nbsp "&#x00A0;">
    ]>
    <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxml="urn:schemas-microsoft-com:xslt"
            xmlns:umbraco.library="urn:umbraco.library"
            exclude-result-prefixes="msxml umbraco.library">
    
      <xsl:output method="xml" omit-xml-declaration="yes"/>
    
      <xsl:param name="currentPage"/>
    
      <xsl:template match="/">
    
        <xsl:apply-templates select="umbraco.library:GetMedia($currentPage/data[@alias='media'], 1)Page, 1)" mode="media" />
    </xsl:template> <!-- ERROR --> <xsl:template match="error" mode="media"> <xsl:comment> <xsl:value-of select="." /> </xsl:comment> </xsl:template> <xsl:template match="node" mode="media" /> <!-- IMAGE --> <xsl:template match="node[@nodeTypeAlias='Image']" mode="media"> <xsl:if test="count(data) &gt; 0 and string(data[@alias='umbracoFile']) != ''"> <img src="{data[@alias='umbracoFile']}" height="{data[@alias='umbracoHeight']}" width="{data[@alias='umbracoWidth']}" alt="{@nodeName}" /> </xsl:if> </xsl:template> <!-- FOLDER --> <xsl:template match="node[@nodeTypeAlias='Folder']" mode="media"> <xsl:if test="count(node) &gt; 0"> <ul title="{@nodeName}"> <xsl:for-each select="node"> <li> <xsl:apply-templates select="." mode="media" /> </li> </xsl:for-each> </ul> </xsl:if> </xsl:template> <!-- FILE --> <xsl:template match="node[@nodeTypeAlias='File']" mode="media"> <xsl:if test="count(data) &gt; 0 and string(data[@alias='umbracoFile']) != ''"> <a href="{data[@alias='umbracoFile']}"> <xsl:value-of select="@nodeName" /> </a> </xsl:if> </xsl:template> <!-- PDF --> <xsl:template match="node[@nodeTypeAlias='PDF']" mode="media"> <xsl:if test="count(data) &gt; 0 and string(data[@alias='umbracoFile']) != ''"> <a href="{data[@alias='umbracoFile']}" class="pdf"> <xsl:value-of select="@nodeName" /> </a> </xsl:if> </xsl:template> </xsl:stylesheet>

    however it won't save without errors (though I'm guessing the errors might not be designtime but runtime which I unfortunatelly can't determine due to the lack of XSLT skills:)...

    As for the approach I had in my mind...I thought something like not having to make a document type instance each time I make/upload a media file...you know the document that would contain a media picker that would hold the reference to the media (since the media items on my site might grow and grow and grow in numbers)...I thought something like making a single / or multiple / but still few document instances on my site that would be media containers of some sort...like maybe passing a relative path to the media that should be displayed in the query string and then have that document (or scripts XSTL/JS within it's template) determine what to do to display it properly (like do the flexpaper for pdf, or fwplayer for flv etc.)

    Doable? Thanks again...

    p.s.

    Sorry for the delay in my reply I'm still trying to fix that WLW F2 properties button being disabled for my content channel...must of tried almost everything...but I sure it's something trivial that I just don't see :(((

  • stc 72 posts 101 karma points
    May 27, 2010 @ 07:13
    stc
    0

    Forgot the error...I think it's runtime...but I don't know how to debug it (is there really any good literature to read on the subject of XSLT writing and debugging it in Umbraco out there):

    System.OverflowException: Value was either too large or too small for an Int32. 
    at System.Convert.ToInt32(Double value) 
    at System.Double.System.IConvertible.ToInt32(IFormatProvider provider) 
    at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) 
    at System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltArgument(XmlQueryType xmlType, Object value, Type destinationType) 
    at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args) 
    at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) 
    at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) 
    at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) 
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results) 
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter) 
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results) 
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results) 
    at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results) 
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)
  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 27, 2010 @ 08:43
    Dirk De Grave
    1

    yep, it's a quite common mistake often made... even by more xperienced xslt gurus.

    <xsl:apply-templates select="umbraco.library:GetMedia($currentPage/data[@alias='media'], 1)Page, 1)" mode="media" />

    call to GetMedia() will result in this error specified in your previous post as it won't have any value until "runtime". Best practise is to put an xsl:if statement around the apply-templates call

    <xsl:if test="string($currentPage/data[@alias='media']) != ''">
      <xsl:apply-templates select="umbraco.library:GetMedia($currentPage/data[@alias='media'], 1)Page, 1)" mode="media" />
    </xsl:if>

    Above snippet will prevent the error from being thrown. It's either that or check the 'Skip errors' box.

     

    Hope this helps.

    Regards,

    /Dirk

Please Sign in or register to post replies

Write your reply to:

Draft