Sitecore – Revisiting token substitutions with Snippets

A previous post, token substitutions in the Rich Text Editor using IFormattable , shows an elegant way to replace tokens with Dictionary entries.

As promised, I’ll show a quick and dirty way to make it easy for the content editor to add these new tokens, via snippets.

Instead of populating the snippets drop down in the rich text editor, you could certainly add your own drop down list within the rich text editor but it has a few more steps.  Here’s a great post from jammykam for that: Adding custom drop down list to rich text editor in Sitecore .

This is a bit simpler and lines up with the previous post.

Roll your own EditorConfiguration

Override the SetupSnippets method in your custom EditorConfiguration class that inherits from Sitecore.Shell.Controls.RichTextEditor.EditorConfiguration.

(You can choose to show or not show the snippets for the rich text editor created in the core database: /sitecore/system/Settings/Html Editor Profiles/[your RTE]/Snippets

namespace Sample821.Shell.Controls.RichTextEditor
 public class EditorConfiguration 
           : Sitecore.Shell.Controls.RichTextEditor.EditorConfiguration
 public EditorConfiguration(Sitecore.Data.Items.Item profile) : base(profile){}

  protected override void SetupSnippets()
 // allow the base to set up the snippet structure

 // If you don't want to use any of the snippets that are in the core db ...

 // add your snippets based on your custom template
 Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

 Sitecore.Data.Items.Item parentDictionary =
 master.GetItem("/sitecore/content/SampleSite/Token Dictionary");

 if ((parentDictionary == null) || (!parentDictionary.HasChildren)) return;

 foreach (Sitecore.Data.Items.Item folder in
 //if u have a folder structure ... less expensive than .GetDescendants()
 if (!folder.HasChildren) continue;

 foreach (Sitecore.Data.Items.Item entry in folder.Children)
      if (string.IsNullOrEmpty(entry["Key"])) continue;

      var snippet = string.Format("{{0:{0}}}",entry["Key"]);

      Editor.Snippets.Add(entry["Key"], snippet);

Add a new patch configuration file and change the EditorConfiguration in Sitecore.config to your new one

<setting name="HtmlEditor.DefaultConfigurationType">
  <patch:attribute name="value">

Test the result

The content editor experience is now much easier to handle the tokens.


You may ask .. why not use the snippet to replace the text with the value in the dictionary entry?  That is certainly possible but defeats the purpose of having the dictionary entry in one place … to easily change it … in. one. place.

Combine this with the previous post and you’ll add reusable functionality for your content editing staff.