Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 939 posts 2574 karma points
    Aug 09, 2012 @ 10:20
    Claushingebjerg
    0

    Differentiated rendering of nodes from multi node tree picker

    I have a multi node tree picker on my front page.
    This is used for picking "Teaser Boxes" These boxes are of different document types, and need to have radically different rendering. Doing this in one large xslt with "xsl:choose/xsl:if" is surely gonna get way to messy over time.
    So im looking for a way to split it into a logical structure, thats easy to maintain.

    Listing the nodes selected with the mulit node tree picker goes like this:

    <xsl:template match="/">
      <xsl:if test="$currentPage/teasers/MultiNodePicker/nodeId">
          <xsl:for-each select="$currentPage/teasers/MultiNodePicker/nodeId">
            <xsl:variable name="node" select="umbraco.library:GetXmlNodeById(.)" />      
              <div class="teaser">$node/@nodeName</div>
          </xsl:for-each>
      </xsl:if>
    </xsl:template>

    What im looking to do, is to have templates for each "Teaser" type, and then "magically" join them in a "Render" Xslt file.

    Something like this: (Excuse the absolute lack of any correct syntax)

    <xsl:template match="/">
      <xsl:if test="$currentPage/teasers/MultiNodePicker/nodeId">
          <xsl:for-each select="$currentPage/teasers/MultiNodePicker/nodeId">
            <xsl:variable name="node" select="umbraco.library:GetXmlNodeById(.)" />      
    <xsl:include href="teaserType01.xslt" /> <xsl:include href="teaserType02.xslt" /> <xsl:include href="teaserType03.xslt" />       </xsl:for-each>
      </xsl:if>
    </xsl:template>

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 09, 2012 @ 10:30
    Chriztian Steinmeier
    1

    Hi Claus,

    If they are different Document Types, the only magic needed is the built-in magic of match templates. Really!

    Here's the long explanation :-)

    And here's the shorter one - change the almost correct one you included above, to this one:

    <xsl:template match="/">
        <xsl:if test="$currentPage/teasers/MultiNodePicker/nodeId">
            <xsl:for-each select="$currentPage/teasers/MultiNodePicker/nodeId">
                <xsl:apply-templates select="umbraco.library:GetXmlNodeById(.)" />
            </xsl:for-each>
        </xsl:if>
    </xsl:template>
    
    <!-- Include templates for the different Doctypes -->
    <xsl:include href="teaserType01.xslt" />
    <xsl:include href="teaserType02.xslt" />
    <xsl:include href="teaserType03.xslt" />
    
    And make your include files very simple, like this:
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:template match="TeaserType1">
            <div>
                <xsl:value-of select="@nodeName" />
            </div>
        </xsl:template>
    
    </xsl:stylesheet>
    Don't include the currentPage param line that usually sits at the top - they will use the declaration from your master XSLT.

    /Chriztian

  • Claushingebjerg 939 posts 2574 karma points
    Aug 09, 2012 @ 12:44
    Claushingebjerg
    0

    Once again sir, you show yourself as the master of disaster, the jedi master of the XSLT-alliance. Thanks

    Funny thing is i got the concept of <xsl:include> from pimp my xslt, i just didnt spot the article on multi-node-tree-picker :). soooooo close :)

     

Please Sign in or register to post replies

Write your reply to:

Draft