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.
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.
<msxsl:scriptlanguage="CSharp"implements-prefix="umbraco.page"> <![CDATA[ public static string GetPreviewText(string text, int numCharsToDisplay) { if (text.Length > numCharsToDisplay) { int cutPos = text.IndexOf(".", numCharsToDisplay);
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!
Hi Brandon - welcome to the forum!
Here's a very simple macro to start with:
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
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 " ">
]>
<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>
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
is working on a reply...