Umbraco/Reference/umbraco.library/RenderMacroContent

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Purpose:[edit | edit source]

Run a macro and insert the output into your XSLT.

Arguments:[edit | edit source]

RenderMacroContent(String Text, Int32 PageId)

Text: an encoded version of the template 'macro' code (see below for proper encoding)

PageId: NodeID for the node you want to use for the content for the macro

String Text Encoding Instructions:[edit | edit source]

When you are putting the code for your macro in a template it looks something like this:

<?UMBRACO_MACRO macroAlias="LatestNews" numberOfItems="3"></?UMBRACO_MACRO>

To encode this for the RenderMacroContent function follow these steps:

1. Change '<' to '&lt;'

&lt;?UMBRACO_MACRO macroAlias="LatestNews" numberOfItems="3">&lt;/?UMBRACO_MACRO>

2. Change '>' to '&gt;'

&lt;?UMBRACO_MACRO macroAlias="LatestNews" numberOfItems="3"&gt;&lt;/?UMBRACO_MACRO&gt;

3. Change '"' to '&quot;'

&lt;?UMBRACO_MACRO macroAlias=&quot;LatestNews&quot; numberOfItems=&quot;3&quot;&gt;&lt;/?UMBRACO_MACRO&gt;

4. When you put this into the XSLT, enclose the whole thing in single quotes:

umbraco.library:RenderMacroContent('&lt;?UMBRACO_MACRO macroAlias=&quot;LatestNews&quot; numberOfItems=&quot;3&quot;&gt;&lt;/?UMBRACO_MACRO&gt;'...

Example XSLT Usage:[edit | edit source]

<xsl:value-of select="umbraco.library:RenderMacroContent('<?UMBRACO_MACRO macroAlias="LatestNews" numberOfItems="3"></?UMBRACO_MACRO>', @id)" disable-output-escaping="yes"/>

This string should of course be encoded, so it would look like this:

<xsl:value-of select="umbraco.library:RenderMacroContent('&lt;?UMBRACO_MACRO macroAlias=&quot;LatestNews&quot; numberOfItems=&quot;3&quot;&gt;&lt;/?UMBRACO_MACRO&gt;', @id)" disable-output-escaping="yes"/>

Little Bonus: XSLT to do the encoding for you[edit | edit source]

If you want to be able to just "cut & paste" the standard template macro code into your XSLT and have it encoded properly, add this bit of code to a new XSLT:

<xsl:variable name="Macro"> <xsl:value-of select="{PUT YOUR TEMPLATE MACRO CODE HERE}"/> </xsl:variable> <xsl:variable name="MacroEncoded"> <xsl:value-of select="umbraco.library:Replace(umbraco.library:Replace(umbraco.library:Replace($Macro, '"', '&quot;'), '>', '&gt;'), '<', '&lt;')"/> </xsl:variable>

Then use this to render the encoded XSLT to a test page in your browser (which you can then cut & paste into your XSLT code):

<xsl:value-of select="$MacroEncoded" />


Version 4 Warning[edit | edit source]

Warning, Umbraco 4 macros with new templates have a different syntax however the XSLT extention still requires the old v3 one so if you copy the macro inserted into a v 4 template you need to change it to v3

i.e.

<umbraco:Macro Alias="Name" runat="server"></umbraco:Macro>

becomes

<?UMBRACO_MACRO macroAlias="Name" runat="server"></?UMBRACO_MACRO>

(with escaping)