Jump to content

Umbraco/Samples and Articles/Macros

From Wikibooks, open books for an open world

A macro for getting the url of an image

[edit | edit source]

Go to Developer/XSLT Files, create a new xslt file GetImageURL with the following content:

<?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:variable name="imageID" select="/macro/imageID/node/@id"/>

<xsl:template match="/">
  <xsl:if test="$imageID != ''">
    <img>
      <xsl:attribute name="src">
        <xsl:value-of select="umbraco.library:GetMedia($imageID, 'false')/data [@alias = 'umbracoFile']"/>
      </xsl:attribute>
    </img>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

Now go to Developer/Macros, create a new macro, Get Image URL, giving it the alias GetImageURL. In Use XSLT File, select the XSLT file you just created.

On Parameters, add a new parameter with the alias imageID and name Image ID. The type should be set to mediaCurrent and Show should be checked. Save the macro and your home!