ICYMI: Blogged: Set up an #Umbraco 5 site on a new and FREE Amazon EC2 instance http://t.co/uWZGetZI /cc @yodasmydad with link this time :)

follow me on

A general purpose Umbraco ImageGen solution using xsl call-template instead of RenderMacroContent

Friday, August 01, 2008 by David Conlisk

In his comment on my last blog post Hendy wondered whether "instead of calling the RenderMacroContent for another XSLT macro, it would be better practice to include common XSLT directly?". I agree - he's right. This is one aspect of XSLT that I'd come across in other people's code (e.g. XSLTSearch) but hadn't gotten around to using myself. It's a lot cleaner than using RenderMacroContent.

Here is the xslt which will be my reusable code which uses ImageGen to output an image, given an imageId, and the desired height and width of the image to display. Note the <xsl-param bits - that's how you define your parameters in xslt templates.

<?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"/>

<!-- General Purpose ImageGen ShowImage template, can be used anywhere on the site -->
<!-- Takes the id, height and width of the image as parameters and uses IMAGEGEN to display it -->
<xsl:template name="ShowImage">
<xsl:param name="imageId"></xsl:param>
<xsl:param name="width"></xsl:param>
<xsl:param name="height"></xsl:param>
<img>
<xsl:attribute name="src">
<xsl:text>/umbraco/ImageGen.ashx?image=</xsl:text>
<xsl:value-of select="umbraco.library:GetMedia($imageId,0)/data [@alias = 'umbracoFile']"/>
<xsl:text>&width=</xsl:text>
<xsl:value-of select="$width"/>
<xsl:text>&height=</xsl:text>
<xsl:value-of select="$height"/>
</xsl:attribute>
</img>
</xsl:template>

</xsl:stylesheet>

And here is one example of using the code. This could be on a product page, to display the product image associated with the current node (called 'image') with a given width as defined by the parameter to the macro.

<?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:include href="ShowImageGenImage.xslt" mce_href="ShowImageGenImage.xslt"/>

<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 -->
<!-- ShowImage xslt template to display it. Height and width values come from the macro parameters. -->
<xsl:template match="/">

<xsl:call-template name="ShowImage">
<xsl:with-param name="imageId" select="$currentPage/data[@alias='image']"/>
<xsl:with-param name="width" select="$width"/>
<xsl:with-param name="height" select="$height"/>
</xsl:call-template>

</xsl:template>

</xsl:stylesheet>

So there you have it. Two ways of re-using your xslt code. I'm sure there are situations where one is preferable over the other, but right now I think I'd lean towards this being the better of the two.

David

 

If you found this article useful, please click the +1 button!

 

2 comment(s) for “A general purpose Umbraco ImageGen solution using xsl call-template instead of RenderMacroContent”

  1. Gravatar of Warren Buckley


    Warren Buckley says:

    Nice solution David.

    I never realised you could include external XSLT files until now. As I always have the other template I am calling in the same XSLT file.

    I am not 100% sure if I will use the include method or not yet, but for a common template I imagine it can be useful.

    Warren
  1. Gravatar of Adam Perry


    Adam Perry says:

    Very useful, great tip for a library of re-useable functions.

    The "RenderMacroContent" method can still be useful - for instance, to render a .NET or Python macro.

    Adam

Please leave a comment: