3

I want the URL of the current item. I added a template in the rendering variant:

<a href="https://current item url?itemid=$item.ID">Button text</a>

What should I replace "current item url" with?

1 Answer 1

2

Out of the box, you can't do this. You would need to create a custom NVelocity Template Renderer to do this. To create a tool you would need a class with a static method to get the item url, like this:

namespace MyCompany.Foundation.Variants.NVelocityExtensions
{
    public class ItemTool
    {
        public static string GetItemLink(Item item)
        {
            var options = UrlOptions.DefaultOptions;
            return LinkManager.GetItemUrl(item, options);
        }
    }
}

and then you would need to create a pipeline processor to register that renderer like this:

namespace MyCompany.Foundation.Variants.Pipelines.GetVelocityTemplateRenderers
{
    public class AddTemplateRenderers : IGetTemplateRenderersPipelineProcessor
    {
        public void Process(GetTemplateRenderersPipelineArgs args)
        {
            args.Context.Put("itemTool", new ItemTool());
        }
    }
}

Finally, you would need to patch in that processor as part of an include file:

<pipelines>
  <getVelocityTemplateRenderers>
    <processor type="MyCompany.Foundation.Variants.Pipelines.GetVelocityTemplateRenderers.AddTemplateRenderers, MyCompany.Foundation.Variants" 
               patch:after="processor[@type='Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.GetVelocityTemplateRenderers.InitializeVelocityContext, Sitecore.XA.Foundation.Variants.Abstractions'] />
  </getVelocityTemplateRenderers>
</pipelines>

Make sure its patched in after the InitializeVelocityContext processor. Then in your NVelocity template you can call it like this:

<a href="https://$itemTool.GetItemLink($item)?itemid=$item.ID">Button text</a>

You can read more about this here: https://michaellwest.blogspot.com/2017/04/custom-rendering-variant-token-tool-for-sxa.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.