Razor Mediator

The story is almost the same from client to client who is using Tridion 5.3 or higher.  They love Compound Templating and like the easiness of the Dreamweaver syntax… however, the power and flexibility of Dreamweaver Templates leaves them feeling like there is just something completely lacking.  How can they get the fields from a Component Link?  How can they get the TemplateRepeatIndex value from an outter repeating region?  How can they do. . . the list of questions and possible frustration just continue to grow from there.

Although a powerful and slick XSLT Mediator already exists on the Tridion eXtensions web site, unless you are comfortable and proficient with XSLT, you may just have an easier time sticking with Dreamweaver Templates and writing C# Template Building Blocks and Custom Functions to get the job done.  Having done a lot of work with ASP.NET MVC, I was really fond of the Razor Templating syntax, and thought it would be pretty cool if we could use that syntax for Tridion templating.  And so a few months ago, I began developing a Razor Mediator, which has now been reviewed and released by SDL Tridion as of the beginning of January.

The mediator can be found at the eXtension site, and the latest code with fixes can be found on its Google Code site.  (Btw, if you want to be a tester or developer on the Razor Mediator project, feel free to contact me and sign up!)

If you haven’t gone over to one of the sites and want to see a quick Razor syntax example, look no further:

<div class=”@Component.Fields.NewsStyle”>
    <img src=”@Fields.HeaderImage.ID” alt=”@Fields.HeaderImage.AltText” />
    @* Note that Fields is just a shortcut to Component.Fields *@
    <h5>@Fields.NewsDate.ToString(“dd/MM/yy hh:mm”)</h5>
    <div class=”body-text”>
    @* Now we'll loop over a ComponentLink field and grab the component title and a field *@
    @foreach (dynamic linkItem in Fields.RelatedItems) {
        <li>@linkItem.Title - @linkItem.Fields.Summary</li>

The above example shows a typical Component Template.  Notice how easy it is to grab the fields of a multi-valued Component Link field?  Also notice how you can easily format a DateField?  Best yet, notice that when you get the value of a field, the Razor Mediator already knows the field type.  Component Link fields return Components (well, ComponentModels), Text and RichText fields return strings, Date fields return DateTimes, and so on.  No need to worry about your templators needing to learn about casting here.

    <title>@(Page.MetaData.HasValue("BrowserTitle") ? Page.MetaData.BrowserTitle : Page.Title)</title>
    <meta name="description" content="@Page.MetaData.MetaDescription" />
    <meta name="keywords" content="@Page.MetaData.Keywords" />
    <div id="page">
    @foreach (var cp in ComponentPresentations) {

The above sample demonstrates a typical Page Template that you encounter, where one would want to get the Page’s Meta Data fields as well as output the page’s Component Presentations.  The two samples really only scratch the surface of features and functionality that the Razor Mediator has to offer.  From convenient features to utility functions and even the ability to customize and extend the mediator through the configuration, the mediator will hopefully allow you to do more while writing far less.  And did I mention the installer?

One of the pitfalls of this mediator would probably be the C# style syntax for the templating.  Although developers and programmers might adapt ever so quickly, designers and non-programmer templators might have a bit more difficulty while learning it.  Though, as one of my non-programming designer co-workers said, “Despite the higher learning curve, its much better than working with Dreamweaver Templates.”

For the next few weeks, I’ll be writing some more articles and tutorials regarding the Razor Mediator which will include some cool and advanced tricks that you can do with it, as well as some sneak peeks into the next versions of the Razor Mediator.