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 *@
    <h2>@Fields.Header</h2>
    <h5>@Fields.NewsDate.ToString(“dd/MM/yy hh:mm”)</h5>
    <div class=”body-text”>
        @Fields.BodyText
    </div>
    <ul>
    @* 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>
    }
    </ul>
</div>

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.

<html>
<head>
    <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" />
</head>
<body>
    <div id="page">
    @foreach (var cp in ComponentPresentations) {
        <text>@cp.RenderComponentPresentation()</text>
    }
    </div>
</body>
</html>

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.

Hello World

Hello World!  This is probably my third or fourth attempt to start a blog of my very own, and hopefully this time around I’ll actually try to make some good attempts to break away from my work and various side projects to commit to writing some entries that will hopefully be useful and helpful to someone somewhere out there in this world.  And at the very least it’ll be a place to post possible solutions that I can come back to at a later time and use as a reference.  You know those rare problems you find yourself in and after much research you have a eureka moment and solve the answer to life’s little riddle.  Only to run into that same rare problem a year or two later down the road, remembering that you did at one point already solve the problem but not quite remembering what you did to solve it….

I can’t promise to be a dedicated  and avid writer, truth be told I’d much rather be hacking away at some creation or project without ever even needing to see the light of day (wouldn’t all of us developers out there?) or spending my time with the family.  And I can’t even promise that this blog attempt number X will go beyond this initial posting.   Actually I’m not even really sure what direction I intended this paragraph to head… I guess all that I can really hope for is that I’ll be a more than a once a year poster.

Well reader, here’s to the journey ahead.  May it be fun filled with lots of learning experiences.