Creating dynamic labels within SDL Tridion content

Configuration Component

The configuration component is a simple content schema which allows the embedding of ‘Key/Value‘ pairs.  This is easily achieved using SDL Tridion’s embedded schemas.

With the following simple text fields:

A configuration component can now be created based on this schema, as shown below:

Great, so now what we need is a way to map the key value from our component to the value placed into the rich-text field thanks to our GUI extension.

Resolving the dynamic label

There are two main ways to resolve this content:

Rendering / Resolving At publish time

To achieve this we have a Template Building Block that parses the rendered content which finds all $$keywords$$ within the content stored in the configuration component.

The main advantage of this implementation is that it is really easy to manipulate the rendered content in SDL Tridion and quickly build this functionality.

The massive disadvantage is that should a dynamic value change, then you will need to republish any page that makes use of it.

Should you wish to go down this route, the following snippets will be relevant in your TBB code:

foreach (Match matchingElement in Regex.Matches(outPut, @"(\$\$[^\$]+\$\$)"))
{
    string resolvedVariable = GetReplacementValue(rlContent, xmlNsMgr, matchingElement.Value);
    Logger.Info("replacing variable: " + matchingElement.Value + " with : " + resolvedVariable);
}

The variables to note are:

  • outPut -  is the actual output item from template package
  • rlContent – the replacement Component object – mine is passed to the package from the publication metadata via another TBB.
  • @”(\$\$[^\$]+\$\$)”) – The regular expression to find all $$keyword$$ values in the output – (note: I wouldn’t actually recommend the $$ as the identifier, but this was legacy :))
  • GetReplacementValue() – The function which searches the component for the actual replaceable value.
private string GetReplacementValue(XmlDocument contentXml, XmlNamespaceManager ns, string value)
{
XmlNode replacementKeyNode = contentXml.SelectSingleNode("descendant::replacement_values[replacement_key='" + value + "']/replacement_value", ns);
return replacementKeyNode.Value;
}

Note: The above snippet includes a lot of stripped out code checking and error reporting to make it easier to see the replacement.

Code this all up and upload into SDL Tridion as a new TBB, which can then be ran against your content.

I typically drop it my own ‘Default Finish Actions’ TBB, but at the end of a Page Template should do the trick :)

Rendering / resolving at presentation time

Resolving at publish time is definitely the way to go with this sort of implementation.  It’s better to edit the content, publish the configuration, then have your website / application perform the rendering.  Here’s how we’ve done it in our .NET website.

  • The configuration component such is associated to a component template that is publishing out as an XML document.

     

  • Our SDL Tridion dynamic variables are parsed by the TBB into a .NET <asp:literal id=”company_name” runat=”server”> Note: be sure you catch if you use the same term more than once in your output.
  • The .NET application supplies a literal value by looking up the content from the published XML.

3 thoughts on “Creating dynamic labels within SDL Tridion content

  1. Nice! The biggest gotchas I see when making extensions is that users expect all the Tridion functionality to apply. Where Used should work, but also BluePrinting and localization, Content Port between systems, the security model (users are not admins), and especially item changes need to work going forward.

    Using Tridion’s keywords lets us update their display names and you have a way to make Where Used work. You can even localize these if needed. The customer should be careful with changing the inserted values, but I’m sure you’ll have a follow up post when they ask to change $$some_value$$ to $$some_new_value$$ everywhere. :-)

    Minor point: I’d consider swapping key and description, but otherwise looks great. Where can we show you some love on StackOverflow?

  2. @Alvin – You can show me some love by going and upvoting everything i ever did on SO :)http://stackoverflow.com/users/1221032/johnwinter

    In terms of a user localising / changing this information you’re right. It would probably have been better to use a keyword key as this can’t be changed, but thebusiness users wish to have this localisation flexibility. They are a good team though, so i’m not too worried here :)

  3. Very Nice. Thanks.
    I’m interested in how can this be extended to display different labels according to visitor’s context (claims in ADF). e.g.: visitors from UK will see price in pounds (£).
    I’m not that familiar with SmartTarget but it does not seem to easily fit since SmartTarget uses Regions as placeholders for content and I don’t think it is feasible to embed regions within a rich text field.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>