1

I'm using Sitecore 9 and SXA 1.7.

I have a template with a "General link with search" field, and I created several items of that template where I need to "insert external link", after that I set the URL and Link Description.

I am creating a rendering variant for this, in it, when I use "Field" to render the "external link" field, it shows me the Link Description I set earlier.

My question is this : Is there a way using the rendering variant to show custom text of mine instead of what is inside the link description, but have that new text still work as a functional link?

In other words : Can I change the displayed anchor name but still keep it a functional link to the URL?

1 Answer 1

2

We had the same issue a while ago and solved it by using Nvelocity. Might not be the best solution (if there is a nicer one I would also love to see it) but it works.

What I did: in your variant, don't use Field but Template (for a NVelocity template). In the template you can do something like this:

#set ($url = $item.Fields.YourLinkField.ToString())
#if ($url != "")
<a href="$url" target="_blank">YOUR TEXT</a>
#end

The "if" construction is to make sure we don't display an empty link when nothing was set. Replace "YourLinkField" with the name of the link field in your template. If you need to add more logic, you can..

The template is not shared, so you could translate the text if needed.

One step further

If you want to extend this -like I did- and want the editor to change the text more easliy you can create your own NVelocity token and use something like this:

#set ($url = $item.Fields.YourLinkField.ToString())
#if ($url != "")
<a href="$url" target="_blank">$translationTool.GetTranslation("Key")</a>
#end

The $translationTool.GetTranslation("Key") refers to custom code and might get the value from the Dictionary (or anywhere you want).

To create such a custom variant token, you can check the blog from Michael West who wrote a good article on it: https://michaellwest.blogspot.com/2017/04/custom-rendering-variant-token-tool-for-sxa.html You need to add a processor to the getVelocityTemplateRenderers pipeline.

3
  • in YourLinkField, should I insert the name of the field as it is in solr? because I tried writing it as it is in sitecore but it didn't work Commented Jul 15, 2018 at 13:12
  • Should be as is in your Sitecore template. That name should be without spaces btw, don't know how to do it when there is a space in the name.
    – Gatogordo
    Commented Jul 15, 2018 at 13:16
  • My field is called WebExternalLink, I tried using your code in a template and replaced the YourLinkField with WebExternalLink, but for some reason it doesn't work.. ends up showing me broken text in the variant :/ is there anything i should check if i'm missing? Commented Jul 16, 2018 at 9:42

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.