Copied to clipboard

Flag this post as spam?

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


  • Brandon 2 posts 22 karma points
    Oct 22, 2012 @ 17:11
    Brandon
    0

    Display Children Content On Parent Page

    I am wondering if anyone has any sample code of a macro that will display children content on a parent page. For example if you have three children content pages under the parent page and you want to render the first 200 characters of each children page on the page. I am new to xslt and do not know where to start. I have done alot of searching and have not found anything that will work for what I need as described above.

    Also I do not want to have to import DLL's to get this to work (ex. uCompnent), was just looking for straight XSLT macro to get this to work.

    Thanks for your help!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 22, 2012 @ 19:46
    Chriztian Steinmeier
    0

    Hi Brandon - welcome to the forum!

    Here's a very simple macro to start with:

    <?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" />
    
        <xsl:template match="/">
            <!-- Process child pages -->
            <xsl:apply-templates select="$currentPage/*[@isDoc]" />
        </xsl:template>
    
        <!-- Template for a single page -->
        <xsl:template match="*[@isDoc]">
            <div class="page">
                <h2><xsl:value-of select="@nodeName" /></h2>
                <p>
                    <xsl:value-of select="substring(umb:StripHtml(bodyText), 1, 200)" />
                </p>
            </div>
        </xsl:template>
    
    </xsl:stylesheet>

    Note: It assumes the alias of the RichText editor property on your pages is "bodyText" - and note that in order to render the first 200 characters "safely" (without accidentally chopping off in the middle of a <strong> tag or similar), we strip all the HTML first. This is usually OK for summaries like this, though.

    Hope it helps,

    /Chriztian

  • Jon Dunfee 199 posts 468 karma points
    Oct 22, 2012 @ 19:58
    Jon Dunfee
    0

    I've done this for many applications from FAQs to News Libraries, etc...  Hopefully this can give you a jumpstart:

    <?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:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library"
      xmlns:umbraco.page="urn:umbraco.page"
      exclude-result-prefixes="msxml umbraco.library umbraco.page">


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

      <xsl:param name="currentPage"/>

      <xsl:variable name="parentId">
        <xsl:choose>
          <xsl:when test="/macro/parentPage != ''">
            <xsl:value-of select="/macro/parentPage" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$currentPage/@id" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>    
        
      <!-- The Pages to iterate over -->
      <xsl:variable name="documents" select="umbraco.library:GetXmlNodeById($parentId)/descendant-or-self::* [@isDoc]" />
        
      <!-- Number of characters to display in the preview -->
      <xsl:variable name="numCharsPreview">
        <xsl:choose>
          <xsl:when test="$currentPage/ancestor-or-self::*/numPreviewChars != '' and number($currentPage/ancestor-or-self::*/numPreviewChars) != 'NaN'">
            <xsl:value-of select="$currentPage/ancestor-or-self::*/numPreviewChars"/>
          </xsl:when>
          <xsl:when test="string(/macro/numCharsPreview) != ''">
            <xsl:value-of select="/macro/numCharsPreview"/>
          </xsl:when>
          <xsl:otherwise>255</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:template match="/">
      
      <ul>
        <xsl:for-each select="$documents/* [@isDoc]">
          <li>
            <textarea><xsl:copy-of select="." /></textarea>
            <xsl:value-of select="umbraco.page:GetPreviewText(umbraco.library:StripHtml(./pageContent), $numCharsPreview)" disable-output-escaping="yes" />      
          </li>
        </xsl:for-each>
      </ul>

      </xsl:template>

      <msxsl:script language="CSharp" implements-prefix="umbraco.page">
        <![CDATA[
            public static string GetPreviewText(string text, int numCharsToDisplay)
            {
                if (text.Length > numCharsToDisplay)
                {
                    int cutPos = text.IndexOf(".", numCharsToDisplay);

                    if(cutPos > 0)
                        text = text.Substring(0, cutPos + 1);
                }
                return text;
            }

    ]]>
      </msxsl:script>
    </xsl:stylesheet>
     

  • Brandon 2 posts 22 karma points
    Oct 22, 2012 @ 20:51
    Brandon
    0

    Ahh perfect, hats of to the both of you! I have it working now with a little tweaking to fit what I needed. Thanks much for your help!!

     

    Cheers,

    Brandon

Please Sign in or register to post replies

Write your reply to:

Draft