Content tokens

Current version: 9.3

Content tokens let you reuse lines of text and/or numbers. This can be convenient when, for example, you want to reuse prices, copyright notices, version numbers, or locations. Content tokens are content items that have a key and a value and can be referenced in fields on other items. They are stored in the Data folder of your SXA site: sitecore/Content/<tenant>/<site>/Data.

The logic of content tokens replacement is implemented in the RenderContentToken processor of the renderField pipeline. You can reference a content token in a rich text, single-line text, and multi-line text field by using the $ key and the name of the content token in parentheses.

When you have inserted the content token, you can recognize it by the key value and the gray background.

Content token in gray.

In HTML, it is wrapped in a span with some additional attributes. In the HTML of a rich text editor, it is possible to insert content tokens as follows:

RequestResponse
<span class="xa-variable" contenteditable="false" data-variableid="{7B10FC53-C296-41B9-8D09-AE5916154438}">Parking</span>

In this case, the RenderContentToken processor replaces the whole span element with the relevant value of the content token matched by its ID. Additionally, you can use the content token as a $(key) in a rich text, single-line text, and multi-line text field. The item is taken as a result of the following search query:

RequestResponse
ID itemId = Context.Database.DataManager.DataSource.SelectIDs(Templates.ContentToken.Fields.Key, variableKey).FirstOrDefault();
Item item = Context.Database.GetItem(itemId);

If you have several results for this query, any of them might be returned and its value will be placed into the text.

To change the logic, you must:

  1. Create a custom project with implementing the RenderContentToken processor of the renderField pipeline.

  2. Rewrite the GetTextVariableValue function and add your custom logic of content token item mapping.

  3. Build dll into the bin folder of your website.

  4. Patch the config file, rewriting the following processor with your custom processor:

    RequestResponse
    <processor type="Sitecore.XA.Feature.ContentTokens.Pipelines.RenderField.RenderContentToken, Sitecore.XA.Feature.ContentTokens" resolve="true" />

    You must make sure that the config file uses the showconfig.aspx page correctly.

Do you have some feedback for us?

If you have suggestions for improving this article,