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...

REL Standard Tag Library

The RSTL is a library of REL tags providing standard functionality such as iterating collections, conditionals, imports, assignments, XML XSLT transformations, formatting dates, etc. RSTL distributable is available on my Google Code page under  REL Standard Tag Library . Always use the latest JAR . This post describes each RSTL tag in the library explaining its functionality, attributes and providing examples. For understanding the way expressions are evaluated, please read my post about the  Expression Language used by REL Standard Tag Library . <c:choose> / <c:when> / <c:otherwise> Syntax:     <c:choose>         <c:when test="expr1">             Do something         </c:when>         <c:when test="expr2">             Do something else         </c:when...

Publish Binaries to Mapped Structure Groups

Today's TBB of the Week comes from the high demand in the field to publish binary assets to different mapped Structure Groups. By default SDL Tridion offers two ways of publishing binaries: All binaries publish to a folder defined in your Publication properties; All binaries rendered by a given template publish to a folder corresponding to a given Structure Group; In my view, both cases are terrible, over-simplified and not representing a real use-case. Nobody in the field wants all binaries in one folder and nobody separates binary locations by template. Instead, everybody wants a mapping mechanism that takes a binary and publishes it to a given folder, defined by a Structure Group, and this mapping is done using some kind of metadata. More often than not, the metadata is the TCM Folder location of the Multimedia Component. I have seen this implemented numerous times. So the solution to publish binaries to a given location implies finding a mapping from a TCM Folder to a...