Razor Mediator


The Razor Template Engine has been introduced into the .NET community with much success, and you'll especially find it common in almost every ASP.NET MVC project you come across now. If you are not familiar with .NET and the Razor Templating syntax, then perhaps you have heard of Velocity? Razor has been compared to Java's Velocity template engine. But why is it so popular? Razor allows an easy to use syntax (pretty much C# syntax) for template scripting, but with a clean easy to read look and feel (so no, we're not taking a step back into the days of VBScript templates!).

Limitations of Dreamweaver Templates

Tridion comes with Dreamweaver Templating out of the box, and those are widely used amongst the Tridion projects out there. But Tridion supports limited functional constructs inside DWTs. Below is an incomplete list of some of these limitations:

  1. ComponentLink Fields. You can't directly access the fields of a Component from a ComponentLink field in DWT. You end up either having a TBB that pushes these values to the package, or by using a Dreamweaver Custom Function (like Nuno's Dreamweaver Get Extensions, which is a must in my opinion when working with DWTs).
  2. TemplateRepeatIndex. You can only get this value of the TemplateRepeat loop that you are working with (ie, can't get the outer TemplateRepeatIndex value).
  3. Nested Repeating Regions. Working with nested repeating regions (e.g. embedded schemas) is not intuitive to many developers first working with DWT templates.
  4. Else Statement. There is no else statement in Dreamweaver templates. You have to write TemplateIf multiple times using negation. (Though there is a ternary expression you can use when outputting variables.)
  5. Whitespace. Although not too big of an issue, it does bother some the amount of whitespace that DWTs output.
  6. Control over output formatting. If you want to have Date formatting for example, you have to write code (TBB or Custom Function) to handle it for you.

The Power of the Razor Mediator

With Razor Templating you have the power of .NET available inside your Razor Templates. Using the base Tridion Razor Template and utilities that comes with the Razor Mediator, your scriptlets have quick and easy access to most of your Template Package items and fields right at your fingertips. Because you are now able to do more and write less, the templating process can be done quicker than ever before.

Razor is a strongly typed, case sensitive, scripting language. For example, for a Tridion DateField, you can format the date right from the template using <span>@Fields.NewsDate.ToString(�MM/dd/yyyy�)</span>. For a Tridion KeywordField, you have access to all of the Tridion keyword fields like <span>@Fields.SomeKeywordField.Title</span>. Want to capitalize your header field? <span>@Fields.Header.ToUpper()</span>. What about accessing fields of Component Links? <div>@Fields.SomeComponentLink.FieldName</div>. You can even go as deep as you want with Component Links. <div>@Fields.SomeComponentLink.AnotherComponentLink.FieldName</div>. You can also add your own scriptlet like helper functions right to your templates if you so desired.

Some of the Tridion types have also been wrapped by Model classes that provide easier functionality of accessing certain data, like Fields and Metadata (dynamic access that always returns the correct type in your templates). For these Model classes, you can always access the underlying Tridion Object through the TridionObject property. For example, @Component.TridionObject.HasUsingItems(). The Component is of type ComponentModel (the special wrapper class), while the TridionObject property actually returns the Tridion Component instance. Most of the Model class' ToString() method writes out the wrapped Tridion item's Title property, with the exception of the ComponentModel, which will write out the Component's Tcm Uri.

Full Documentation

Full documentation is included in the package that includes installation, API reference, and many, many samples of using the powerful razor syntax. You can also download this including the source code directly from the Google code project: code.google.com/p/razor-mediator-4-tridion/.


Installing the Razor Mediator is as simple as running the Installer! See the documentation for more details, as well as information on what exactly is being installed and configured when you run the installer.

Please Test First!

Please make sure to test your work before moving your sections that include functionality of the Razor Mediator to your production environments! Please report any issues that you may find.

... and still developing!

The Razor Mediator is still in development with even more features to come (as well as fixes to anything reported!). Please feel free to join in with the open source project on Google code code.google.com/p/razor-mediator-4-tridion/.

:    Tahzoo
License agreement
Download package
:    razor,  mediator,  templating,  2011,  .NET
:   Yes

About the Author
Alexander Klock
Technical Consultant

SDL Tridion Technical Consultant for Tahzoo