Copied to clipboard

Flag this post as spam?

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


  • David Zweben 268 posts 754 karma points
    Mar 05, 2012 @ 16:48
    David Zweben
    0

    Help modifying an existing XSLT?

    I'm trying to add some blocks of content to my website that will be inserted into multiple pages. I found this how-to on creating resuable content, and it was pretty close to what I wanted, so I followed it. I set it up exactly as it said, with the same names and IDs, and it's working as intended. It doesn't exactly fit my needs, though, and I could use some help modifying it.

    The 'how-to' uses an Ultimate Picker on each back-end page the content will show up on, that requires you to manually select the 'touts' you want to display. I have two pages I want to show the content on: one should list all touts that currently exist, and another should show the newest 5 or so; I don't want to manually manage what shows up with checkboxes.

    How would I remove the Ultimate Picker from the setup, so the XSLT inserts all touts without requiring me to mess with checkboxes on the back-end pages? If someone could also help me get it to display a fixed number of items on certain pages, or set up a back-end control for the number of touts to display, even better. Can anyone point me in the right direction?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 05, 2012 @ 21:44
    Chriztian Steinmeier
    0

    Hi David,

    I'd wipe everything and start from scratch - the way I see it, you need this:

    1. A bunch of "touts" somewhere in a container type in Umbraco (you probably already have this) e.g.:

    Home
    - Touts
    - - Tout "No. 1"
    - - Tout "No. 2"
    etc.

    2. A "Max no. of Touts" (maxNoOfTouts) property on the container document type

    3. A single Macro for rendering the Touts

    4. A "mode" parameter on the macro

    5. XSLT like this in the macro file:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
    
        <!-- Grab the "site root Home node -->
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
        <!-- Select the Touts container node here -->
        <xsl:variable name="touts" select="$siteRoot/Touts" />
    
        <!-- Take the number of Touts to show from the maxNoOfTouts property on that container -->
        <xsl:variable name="maxTouts" select="$touts/maxNoOfTouts" />
    
        <!-- Grab the mode macro parameter  -->
        <xsl:variable name="toutMode" select="/macro/mode" />
    
        <!-- Go, go, go! -->
        <xsl:template match="/">
            <xsl:choose>
                <xsl:when test="$toutMode = 'all'">
                    <xsl:apply-templates select="$touts/Tout">
                        <xsl:sort select="@createDate" data-type="text" order="descending" />
                    </xsl:apply-templates>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:for-each select="$touts/Tout">
                        <xsl:sort select="@createDate" data-type="text" order="descending" />
                        <xsl:if test="position() &lt;= $maxTouts">
                            <xsl:apply-templates select="." />
                        </xsl:if>
                    </xsl:for-each>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
        <!-- Template for a single Tout (used in both modes) -->
        <xsl:template match="Tout">
            <section class="tout">
                <h1><xsl:value-of select="@nodeName" /></h1>
            </section>
        </xsl:template>
    
    </xsl:stylesheet>
    

    On the page where you need all touts displayed, call the macro like this:

    <umbraco:Macro alias="DisplayTouts" mode="all" runat="server" />

    - and on the other pages, to display a fixed number of touts, omit the mode parameter:

    <umbraco:Macro alias="DisplayTouts" runat="server" />
    /Chriztian
  • David Zweben 268 posts 754 karma points
    Mar 07, 2012 @ 22:01
    David Zweben
    0

    Chriztian,

    That code looks promising, but for some reason I can't get it to work. I set up everything as you suggested, and when I go to the page, or do Visualize XSLT, I get no output, regardless of the mode, the page I try to display them on, and how many Touts I have published.

    I think I understand what the code is supposed to do, and I broke down the code to try to isolate the issue; my best guess is that something is wrong with the XPath that is being generated to find the Tout nodes. I tried to write some code to display the contents of a single specific tout, but I could never get anything to show up. I'm just learning XPath and XSLT, so hopefully I'm missing something simple?

    How would I display the contents of "toutTitle" on any individual tout? Maybe if I could get that to work, I could work backwards and fix the whole thing. Below is the contents of the Touts XML from my umbraco.config file. Any further help would be greatly appreciated.

      <Touts id="1200" parentID="-1" level="1" writerID="0" creatorID="0" nodeType="1198" template="0" sortOrder="6" createDate="2012-03-07T10:11:00" updateDate="2012-03-07T10:14:10" nodeName="Touts" urlName="touts" writerName="Web" creatorName="Web" path="-1,1200" isDoc="">

        <maxNoOfTouts>12</maxNoOfTouts>

        <Tout id="1201" parentID="1200" level="2" writerID="0" creatorID="0" nodeType="1199" template="0" sortOrder="1" createDate="2012-03-07T10:11:33" updateDate="2012-03-07T10:11:44" nodeName="Tout 1" urlName="tout-1" writerName="Web" creatorName="Web" path="-1,1200,1201" isDoc="">

          <toutTitle>This is text for the first tout.</toutTitle>

        </Tout>

        <Tout id="1202" parentID="1200" level="2" writerID="0" creatorID="0" nodeType="1199" template="0" sortOrder="2" createDate="2012-03-07T10:11:50" updateDate="2012-03-07T10:11:59" nodeName="Tout 2" urlName="tout-2" writerName="Web" creatorName="Web" path="-1,1200,1202" isDoc="">

          <toutTitle>This is text for the second tout.</toutTitle>

        </Tout>

        <Tout id="1203" parentID="1200" level="2" writerID="0" creatorID="0" nodeType="1199" template="0" sortOrder="3" createDate="2012-03-07T10:12:04" updateDate="2012-03-07T10:12:11" nodeName="Tout 3" urlName="tout-3" writerName="Web" creatorName="Web" path="-1,1200,1203" isDoc="">

          <toutTitle>This is text for the third tout.</toutTitle>

        </Tout>

        <Tout id="1204" parentID="1200" level="2" writerID="0" creatorID="0" nodeType="1199" template="0" sortOrder="4" createDate="2012-03-07T10:12:16" updateDate="2012-03-07T10:12:23" nodeName="Tout 4" urlName="tout-4" writerName="Web" creatorName="Web" path="-1,1200,1204" isDoc="">

          <toutTitle>This is text for the fourth tout.</toutTitle>

        </Tout>

      </Touts>

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 08, 2012 @ 01:30
    Chriztian Steinmeier
    0

    Hi David,

    I can see what 's wrong now - the XSLT assumes your Touts node is below your site's root, but I can see that it's actually at the top level, which means that the touts variable is set to nothing and the rest fails from there.

    You should set the touts variable using either of these instead, and it should all just work:

    <xsl:variable name="touts" select="$siteRoot/../Touts" />

    or:

    <xsl:variable name="touts" select="umb:GetXmlNodeById(1200)" />

    (If you're not using all of my code from above, you may need to use umbraco.library:GetXmlNodeById() instead ...)

    /Chriztian 

  • David Zweben 268 posts 754 karma points
    Mar 08, 2012 @ 17:58
    David Zweben
    0

    Chriztian, 

    This seems to be working perfectly now. You've helped me out greatly, thanks!

Please Sign in or register to post replies

Write your reply to:

Draft