World is now on Opti ID! Learn more

Using 'Dictionary' (aka 'Labels') content within rich text fields

Vote:
 

We are looking to implement 'Dictionary' (aka. 'Labels') functionality for a new client of ours - Where they will have a single 'Dictionary' page containing snippets of text (e.g. "Submit button", "Number of widgets sold", etc.) that can then be added just once and referenced throughout the site.

We already do this for for a couple of clients.However, the difference with this implementation is that we also want to be able to access these dictionary items to replace placeholders within rich text fields - as well as within Razor templates.

For example, we would add the placeholder [%NumberOfWidgetsSold%] into the middle of a rich text field and it would pick up the associated content from the Dictionary content (see mocked up screenshot below):

Screenshot of rich text field with placeholder

...And also have the following within our Razor views:

Optimizely Dictionary Razor Template Example

 

I found the 'Merging of texts via CMS' forum post, with two links to possible solutions:

However, both of these relate to far earlier versions of Optimizely/Episerver and .Net Framework.

Before I go away and do a deep(er)-dive, is there a common / best practice approach or an up-to-date open source package for doing this in an Optimizely PaaS CMS v12 world please?

(P.S. I am conscious about any performance overhead too, but equally don't want to make the solution too complex.)

Thanks in advance!

#338164
May 15, 2025 13:42
Vote:
 

Perhaps you could create a display template for XhtmlString and in it parse the fragments to replace your placeholders during rendering?

Docs: https://world.optimizely.com/CsClassLibraries/cms/EPiServer.Core.XhtmlString?version=12#EPiServer_Core_XhtmlString_Fragments

#338178
Edited, May 16, 2025 7:48
Jon Williams - May 19, 2025 13:50
Thanks for the answer, Ted. We did consider a HTML Helper to perform the replacement but we need this replacement to work in templating code and plain text fields too, so had to look at other options.
Ted - May 20, 2025 6:52
I might be misunderstanding the requirements, but if you add display templates for both XhtmlString and string, you're able to use the same placeholder logic for all string properties. Preferably the string parsing itself is in its own extension method that can be used anywhere, for example if you need to parse placeholder strings outside of a render context.
Vote:
 

The latest version of the Verndale.ReplacementTokens package you mentioned seems to support CMS 12 and .net 8 so that could be an option if you wanted something ready-built.

If I were writing something from scratch, I'd probably use tag helpers as they should act on both text in templates and content entered in the CMS. I reckon it would be pretty straightforward to get up and running too.

#338186
May 16, 2025 11:23
Vote:
 

Hi Jon,
The  Verndale.ReplacementTokens add-on is exactly what you need. Just install it and follow the setup instructions from the Readme file.

After that, create a token like this:


Then, use that token in any page or block like this:

Let me know if you run into any issues!

#338382
Edited, May 19, 2025 20:49
Vote:
 

Be aware that the action filter approaches are run on every single request. Both parsing and replacing.

I have recently implemented another approach for a client, using a custom FragmentParser. This way the tokens in an Xhtmlstring are only parsed the first time the XhtmlString is loaded. This is generally the heaviest part. Those tokens are then replaced at render-time, using cached data. This is very efficient.

Unfortunately, I don't have sample code to show, yet.

#338406
May 20, 2025 5:49
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.