Is it bad that I just acheived inbox-zero by deleting all my emails?

follow me on

Passing parameters to RenderMacroContent

Wednesday, July 30, 2008 by David Conlisk

First off, here are my references:

http://forum.umbraco.org/yaf_postst2885_RenderMacroContent--Help.aspx

http://en.wikibooks.org/wiki/Umbraco/Reference/umbraco.library/RenderMacroContent

Between the two of these solutions, I eventually got this to work. I was trying to get a general purpose way of using ImageGen, which in the end doesn't make all that much sense as the gains of doing it this way are minor. But anyway, the theory holds for how to pass parameters to your rendered macro.

I have a macro called ShowImageGenImage, which takes an image Id, the height and width of the image and displays it. It doesn't care where in the site the current node is - it's a general purpose macro for spitting out an image of a certain size, given the image ID and the required height and width. This can be used anywhere on the site and for any image.

On each product page in my site, I want to display the image associated with the current node, with a height and width of 200px (the site is laid out one node per product, each product node having an attribute called "image").

Here is my ShowProductImage.xslt. It uses CDATA to encode the macro properly, while allowing the variable values to be inserted. I had tried to encode these directly, but for example my $imageId variable was coming through as the string "$imageId" instead of the actual value, in this case 1063. The height and width are passed in as parameters to the macro, but the $imageId comes from the current node.

Hope that helps!

David

P.S. Yes I will get some styles sorted out for inserting code into the blog, and yes, the RSS feed will appear here eventually...

<?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:umbraco.library="urn:umbraco.library"
exclude-result-prefixes="msxml umbraco.library">


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

<xsl:param name="currentPage"/>
<xsl:param name="width" select="/macro/width"/>
<xsl:param name="height" select="/macro/height"/>

<!-- For use on the product page. Passes the image ID (alias is "image") of the current node to the -->
<!-- ShowImageGenImage macro to display it. Height and width come from the template. -->
<xsl:template match="/">

<!-- create the escaped macro string with variable values set to be passed to RenderMacroContent, below -->
<xsl:variable name="macro">
<![CDATA[<?UMBRACO_MACRO macroAlias="ShowImageGenImage" imageId="]]>
<xsl:value-of select="$currentPage/data[@alias='image']"/>
<![CDATA[" height="]]><xsl:value-of select="$height"/>
<![CDATA[" width="]]><xsl:value-of select="$width"/>
<![CDATA["></?UMBRACO_MACRO>]]>
</xsl:variable>

<xsl:value-of select="umbraco.library:RenderMacroContent($macro, $currentPage/@id)" disable-output-escaping="yes"/>

</xsl:template>

</xsl:stylesheet>
Bookmark and Share

4 comment(s) for “Passing parameters to RenderMacroContent”

  1. Gravatar of Heather Floyd - Web Developer for Sole-proprietors


    Heather Floyd - Web Developer for Sole-proprietors says:

    Hi David, that is great!
    One thing I noticed while doing some of this today... If you want to pass in a HTML string as one of the macro params, you need to encode it first:

    xsl:value-of select="umbraco.library:HtmlEncode($BodyText)")

    ~Heather
  1. Gravatar of Kenneth Solberg


    Kenneth Solberg says:

    Good practice or not - it's really annoying that included XSLT files gets cached and not "refreshed" until you touch the XSLT including it.
  1. Gravatar of Hendy


    Hendy says:

    Hi, I like the way of using CDATA to save manipulating the macro string, although I'm left wondering whether it is better practice to refactor the XSLT such that insead of calling the RenderMacroContent for another XSLT marco, it would be better practice to include common XSLT directly ?
  1. Gravatar of David


    David says:

    Hi Hendy, thanks for the comments. I think you're right, it's not something I'd thought of to be honest. I've written another blog post here: A general purpose Umbraco ImageGen solution using xsl call-template instead of RenderMacroContent where I've solved the same problem without using RenderMacroContent. It's much cleaner I think, much neater.
    Thanks for pointing me in the right direction!

    David

Please leave a comment: