I've occasionally seen the requirement for tokenised content in Episerver. By tokenised I mean the ability to insert a token such as [ContactUsEmail] or [Telephone] site wide and have it populated based on values set at a site/section level. This allows site editors to set the value of a token once and have it populated across the site.
To implement the list of tokens I am going to use an Episerver PropertyList to set up the tokens and the replacement values. It should be noted the code below is based on the PropertyList released in Episerver 11 that removed beta flag.
Performing the replacement is done by an ActionFilterAttribute. This is applied just before the content is output and after all rendering has been performed. Full code the for the implementation can be found on this Gist.
There are two ways that the tokens can be applied to a site:
1. Add tokenisation to all pages
The following initialisation module will adds the [TokenReplacement] filter attribute to all controllers on the site. This means any content rendered anywhere on the site will have the tokenisation applied:
2. Tokenise on certain controllers
If more fine grained control is required then then tokens can simply be added to individual controllers as shown on the StartPage controller below:
The outcome is that editors can use tokens when creating content to ensure they are always pulling from a single source of truth when creating content:
Other points to consider
This is a powerful feature but can have wide ranging consequences. Therefore the following points are worth considering:
- The tokens values are inserted at runtime by ASP.net filter so do not respect the Episerver cache.
- The token replacement is done on the raw HTML meaning it can affect inline style or script as well as content. This may be a desirable or undesirable effect!
Using some standard Episerver components and some ASP.net smarts we can replace tokens site wide and make editor’s lives easier and convenient when they need to set consistent, site wide values.
Is this a requirement you’ve seen before? If you have any feedback, issues or ideas please let me know in the comments below or on @davidknipe.