Bogdan
Bogdan

Reputation: 913

Displaying value of NameValueList fields in XSLT rendering

Is there any extension method that could be used in a Sitecore XSLT rendering for aiding in the display of NameValueList field types (their values), or am I required to create my own extension method for this operation?

Upvotes: 1

Views: 288

Answers (1)

Matt J.
Matt J.

Reputation: 1830

There is no built-in extension method for displaying data from a NameValueList field. Only really ones like are included out of the box.

You can build your own using something like this. Make a new class for the custom rendering code.

namespace Sitecore7.Custom
{
    public class XslExtensions : Sitecore.Xml.Xsl.XslHelper
    {
        public string RenderNameValueList(string fieldName)
        {
            Item item = Sitecore.Context.Item;
            if (item == null)
            {
                return string.Empty;
            }
            List<string> entries = new List<string>(item[fieldName].Split('&'));
            string rendering = string.Empty;
            if (entries.Count > 0)
            {
                rendering += "<table>";
                foreach (string entry in entries)
                {
                    if (entry.Contains("="))
                    {
                        string name = entry.Split('=')[0];
                        string value = entry.Split('=')[1];
                        rendering += "<tr><td>" + name
                          + "</td><td>" + value + "</td></tr>";
                    }
                }
                rendering += "</table>";
            }
            return rendering;
        }
    }
}

Then you'll need to add a reference to it in a config file. You edit the sample one at /App_Config/Include/XslExtension.config if you don't have one already.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <xslExtensions>
      <extension mode="on" type="Sitecore7.Custom.XslExtensions, Sitecore7"
        namespace="http://www.sitecore.net/sce" singleInstance="true"/>
    </xslExtensions>
  </sitecore>
</configuration>

Then at the top of you XSLT document, in the stylesheet section, add something like:

xmlns:sce="http://www.sitecore.net/sce"

...and include "sce" in the exclude-result-prefixes parameter.

Now you can finally reference the method in your rendering:

Named value pair rendering here:<br />
      <xsl:value-of select="sce:RenderNameValueList('MyFieldName')"
        disable-output-escaping="yes"/>

This can be improved on plenty, but should get you started.

Upvotes: 2

Related Questions