World is now on Opti ID! Learn more
World is now on Opti ID! Learn more
Perhaps you could create a display template for XhtmlString and in it parse the fragments to replace your placeholders during rendering?
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.
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!
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.
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):...And also have the following within our Razor views:
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!