Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
Experience Sitecore! | SXA built-in token tools to be used with rendering variant templates

Experience Sitecore!

Martin Miles on Sitecore

SXA built-in token tools to be used with rendering variant templates

You might came across Variant Template Field when going through rendering variants insert options and wondered, what is that? Variant Template Field is a special field that uses NVelocity templates (supported by Sitecore.NVelocity.dll) in order to render the value.


One of the MVPs - Michael West - has briefly mentioned built-in token tools coming with SXA. I decided to take a look on these tools. I found them in Sitecore.XA.Foundation.Variants.Abstractions.dll under Sitecore.XA.Foundation.Variants.Abstractions.NVelocityExtensions namespace. Here's their code:

public class DateTool
{
    public string Format(DateTime dateTime, string format) => dateTime.ToString(format);

    public string Format(string dateTimeString, string format)
    {
        DateTime time = DateUtil.IsoDateToDateTime(dateTimeString, DateTime.MinValue);
        if (time != DateTime.MinValue)
        {
            return time.ToString(format);
        }
        return dateTimeString;
    }
}

and

public sealed class NumberTool
{
    public string Format(double value, string format) => value.ToString(format, CultureInfo.InvariantCulture);
    public string Format(int value, string format) => value.ToString(format, CultureInfo.InvariantCulture);
    public string Format(float value, string format) => value.ToString(format, CultureInfo.InvariantCulture);
}

DateTool is used internally by another field type - VariantDateField, that's how it works. Let's say I have a template:


 

and it renders to: 


But you may also use it from templates (since it is already mapped) for example combining with your custom tools (not just this one line):


resulting with:


It is also briefly mentioned in the official documentation.

blog comments powered by Disqus