Skip to main content

Writing My Own Custom REL Tag

This walkthrough describes an implementation of an SDL Tridion custom REL tag. The tag has been written in SDLTridion 2013, but it is also applicable to Tridion 2011.

Tridion comes with a number of default REL tags, but you can also add your own custom tags. A REL tag can appear inside a Page or Component Presentation content, and it has to be published to the Content Delivery Database. These tags are executed on the Content Delivery side (presentation server) in one of the following situations:
  • using the ComponentPresentationAssembler: when retrieving the content of a DCP;
  • using the PageContentAssembler: when retrieving the content of a Page;
  • using Content Delivery Web Service (OData), when retrieving either a Page or a DCP;

Prerequisites

In my approach, I chose to use the Content Delivery Web Service (OData) approach. This means I had to install and configure an OData service. You can do so by following the installation guide from SDL Live Content.

I also needed a Deployer that publishes everything to the CD DB. I went on and installed and configured one. For details about Deployer installations, you could consult the online documentation portal as well.

Good Part First -- The Code

A custom REL tag has to implement interface com.tridion.tcdl.TagRenderer. The example tag I implement is located in package com.tridion.extensions.customrenderer, and it only outputs the string "Hello, world!". The code below shows just that. Yes, it is Java!

public class HelloWorld implements TagRenderer {

    @Override
    public String doEndTag(Tag tag, StringBuffer content, TransformContext transformContext,
            OutputDocument document) throws TCDLTransformerException {
        content.append("Hello, world!");
        return content.toString();
    }

    @Override
    public int doStartTag(Tag tag, StringBuffer content, TransformContext transformContext,
            OutputDocument document) throws TCDLTransformerException {
        return Tag.CONTINUE_TAG_EVALUATION;
    }

    @Override
    public boolean requiresCodeBlock(TransformContext transformContext,
            OutputDocument document, Tag tag) {
        return false;
    }
}

Next, compile this class into a JAR and place it in the /lib folder among your other JARs part of your Content Delivery stack. In my case, as I'm running an OData service as a Java web application, I copied it to the [ODataWebRoot]/WEB-INF/lib folder.

The Configuration

First we need to define a mapping between a certain namespace, a tag name and its implementing class. This is used by the TCDL rendering mechanism to know that whenever it encounters that namespace and tag name, it should execute the mapped implementation class.

Create an XML file, name it my-custom-tag-bundle.xml and copy the following XML snippet in it:

<TCDLEngine>
    <Tags>
        <Tag Namespace="rel" Name="HelloWorld">
            <Handler Class="com.tridion.extensions.customrenderer.HelloWorld"
                AllowCodeBlock="false" />
        </Tag>
    </Tags>
</TCDLEngine>

Place this file among the other Tridion CD configuration files in your web application. In my case, I copied it to [ODataWebRoot]/WEB-INF/classes.

Next, we need to reference the XML above in the configuration file cd_dynamic_conf.xml. Edit this file in your web application, and add the highlighted line directly inside the existing TCDLEngine node.

NOTE: There is a bug in cd_dynamic_conf.xsd, which omits to specify TagBundle as a valid child element of TCDLEngine. The example below works, though. Use it!

<TCDLEngine>
    <TagBundle Resource="my-custom-tag-bundle.xml" />

    <Renderer Class="com.tridion.tcdl.TCDLRenderer">
        <Properties>
            <Property Name="prop1" Value="value1" />
            <Property Name="prop2" Value="value2" />
        </Properties>
    </Renderer>
</TCDLEngine>

The CM Part

Create a Dreamweaver TBB that outputs the custom REL tag. You can choose an arbitrary name and namespace. I chose name HelloWorld and namespace rel.
Snapshot of a custom REL tag in a Dreamweaver TBB

Place the Dreamweaver TBB on a compound Page Template called REL PT.

I needed a simple Page that would output my custom REL tag. Create a simple Page, with no Component Presentations on it. Make use of the REL PT.
Snapshot of an SDL Tridion Page using the REL Page Template

Next I needed to publish the Page, so I needed a Publication Target for that. This target would have to be configured to use REL language.
Snapshot of an SDLTridion Publication Target with Target Language REL

At this stage, we can publish the Page and it would end up in the Content Delivery DB. There will be a record basically containing the page content in a CLOB column:

My custom REL tag: <rel:HelloWorld />

Testing it All...

In a browser, fire a request to the CD Web Service, requesting the Page Content for the Page you just published. My URL looks something like this:

http://localhost:8080/odata/odata.svc/Pages(ItemId=974,PublicationId=5)/PageContent

The response is following:
Snapshot of CD Web Service response with executed custom REL tag

Notice the text inside the d:Content element: "My custom REL tag: Hello, world!". It shows how the call to the custom REL tag has been replaced by the value we output from the tag code.


Comments

Popular posts from this blog

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven