/config/xsltExtensions.config 3
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.
You get the option to “get extensions”.
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 .
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.
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(“&rating=G”);
image.Append(“&size=” + size.ToString());
image.Append(“&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
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”>