Sitecore, XSL

Reference to templates


The last couple of weeks I have stumbled on several people referring to a specific template when developing XSLT’s or in some other code. Even Sitecores standard XSL does it:

<xsl:variable name=”home” select=”$sc_item/ancestor-or-self::item[@template=’site root’]” />

It is fine that they finally removed the hardcoded path to /sitecore/content/home, but in my opinion the reference to the specific template ‘site root’ is still bad practice.

When developing a site one should take into account, that the site can be expanded, altered or duplicated later on. In the process of doing so, it is likely that a new template is created, which then inherits from the original template (in this case the ‘site root’). If this is the case all your renderings will stop working, just because you wanted to add a field, change a rendering or in any other way alter the original template.
Instead you should test whether an item is of a given ‘type’ – testing if the template the item is based on is equal to or derives from a specific template. This functionality has even been included in the XSLHelper class in Sitecore 6. Now it is possible to do the test like this:

<xsl:variable name=”home” select=”$sc_item/ancestor-or-self::item[sc:IsItemOfType(‘site root’,.)]”/>

Standard

2 thoughts on “Reference to templates

  1. d(^_^)b says:

    What happens if one is to apply the 1337 filter? Then the code could look like this:

    Since the l33t translator is non-deterministic, the code will sometimes fail, depending on the used translation rule set. One should keep that in mind.

  2. d(^_^)b says:

    Like this:

    <xsl:variable name=”home” select=”$sc_item/ancestor-or-self::item[sc:IsItemOfType(‘s1t3 r00t’,.)]“/>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s