Nibble

/config/xsltExtensions.config

Another config file (check here for /config/Dashboard.config) that can be found in the /config folder of umbraco is the xslExtensions.config.

This file can be used to setup custom xslt extensions.

If you are not familiar with xslt extensions here is a brief intro:

With xslt extensions you can call static .net methods from xslt.

When editing an xslt file and inserting a value.

xsltextensions1

You get the option to “get extensions”.

xsltextensions2

Hitting the button will display a new modal that will list all xslt extensions and their methods.

Default umbraco has a couple of xslt extensions .

xsltextensions3

The most used one would probably be umbraco.library:NiceUrl(Int32 nodeID) , this takes a node id and will output the url of that node.

xsltextensions4

 

Using /config/xslExtensions.config you can also add custom xslt extensions.

Lets look at an example, the blog package for umbraco 4 comes with an xslt extension to get the gravatar image based on an email address.

So step 1 is to write the .net code that can be used. This is just a static method.

 

using System;
using System.Collections.Generic;
using System.Web;
 
namespace Umlaut.Umb.Blog
{
    public class BlogLibrary
    {
        /// <summary>
        /// Gets the gravatar.
        /// </summary>
        /// <param name=”email”>The email.</param>
        /// <param name=”size”>The size.</param>
        /// <param name=”defaultImage”>The default image.</param>
        /// <returns></returns>
        public static string getGravatar(string email, int size, string defaultImage)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(email));
 
            System.Text.StringBuilder hash = new System.Text.StringBuilder();
            for (int i = 0; i < result.Length; i++)
                hash.Append(result[i].ToString(“x2″));
 
            System.Text.StringBuilder image = new System.Text.StringBuilder();
 
            image.Append(“http://www.gravatar.com/avatar.php?”);
            image.Append(“gravatar_id=” + hash.ToString());
            image.Append(“&amp;rating=G”);
            image.Append(“&amp;size=” + size.ToString());
            image.Append(“&amp;default=”);
            image.Append(System.Web.HttpContext.Current.Server.UrlEncode(defaultImage));
           
 
            return image.ToString();
        }
    }
}

 

Next step (after dropping the assembly in the bin) is to setup the xslExtensions.config file.

<?xml version=”1.0″ encoding=”utf-8″?>
<XsltExtensions>
  <ext assembly=”\bin\Umlaut.Umb.Blog” type=”Umlaut.Umb.Blog.BlogLibrary” alias=”BlogLibrary”>
  </ext>
</XsltExtensions>

So you need a new ‘ext’ node with the following attributes:

  • assembly
  • type
  • alias

If the settings are correct it should appear (with the alias setup in the config file) in the xslt extensions modal

xsltextensions5

Last thing todo before you can use it is to declare the namespace.

<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”
    xmlns:BlogLibrary=”urn:BlogLibrary”
    exclude-result-prefixes=”msxml umbraco.library BlogLibrary”>

3 Comments so far

  1. Thomas Höhler on March 11th, 2009

    Hi Tim,

    another really usefull addon. Thanks for shareing.

    Thomas

  2. Aaron Powell on March 11th, 2009

    Just a note to people who like to have closed class design, you cannot hide the default constructor of an XSLT extension, you must ensure that you have constructor which takes 0 arguments (and is public).

    Made that mistake myself! :P

  3. Ismail on March 12th, 2009

    Aaron,

    I got caught with when doing xslt extension by making my class static. The members have to be static but not the class!

    Ismail

Leave a Reply