by

SDL Tridion Razor Mediator: Some Handy Keyword functions

I’ve recently finished a big project with Tahzoo that runs on Tridion 2011 and uses Razor Mediator. There’s a growing number of Tridion  Razor Mediator implementations out there, but this one was a tad different for a few reasons. For one, this project was heavily taxonomy-dependent. Navigation is generated with categories and keywords, and even certain landing pages depend on categories and keywords. So as I developed in the project, I discovered limitations in how version 1.3 accessed the keyword model. Well, Alex and I collaborated on some 1.3.1 methods for working with keyword properties that Razor won’t let you directly access.

So if you’re running Tridion 2011 and Razor Mediator 1.3 or earlier, this post is going to be relevant to you. These are all helper functions or simply public functions which you can use in your next project.

I’m not going to claim origination for all of these Razor helper functions. Alex Klock helped me with two of them.

Keywords with Razor Mediator

Get Parent Keyword

In 1.3 and earlier, there is not a direct way to get the parent keyword of a given keyword. If that’s the case, you can use this function to get the parent keyword of a given keyword. This gets the first keyword that’s returned (as seen by use of the array method : [0]). If you need to trickle on the way up the line, you’ll need to apply this function to each keyword that’s returned, each time.

 @functions{
    public dynamic GetParentKeyword(dynamic kw){
        var parent = kw.TridionObject.ParentKeywords[0];
        return Models.GetKeyword(parent.Id);
    }
}

You can then use this function in the following way:

<tr>
   <th>Keyword</th<th>Parent Keyword</th>
</tr>
<tr>
 <td>@Fields.someKeyword</td><td>@GetParentKeyword(Fields.someKeyword)</td>
</tr>

Not only can you get the Parent Keyword value, but you could also get any content stored in a metadata schema that’s attached to this parent keyword:

@GetParentKeyword(Fields.someKeyword).someMetadataField

Get Related Keywords

If you’re using Razor Mediator 1.3 and earlier, and you want to get related keywords, you can’t do that with by simply doing @SomeKeyword.RelatedKeywords. Yes, it’s a property right there in the XML for a keyword, but it’s just not going to happen. Once I shared my frustration with Alex — and he explained the importance of reading documentation — he was kind enough to put it in his latest build. So, if you want RelatedKeywords prior to 1.3.1, give this a shot:

Keep in mind that unlike the parent keyword function, this will not return a single value. Instead, it’s giving you a list. It’s up to you to figure out how you want to handle that list.

@functions{
    public List<dynamic> GetRelatedKeyword(dynamic kw){
        var relatives = kw.TridionObject.RelatedKeywords;
        List<dynamic> listOfRelatives = new List<dynamic>;();
        foreach (var relative in relatives) {
           listOfRelatives.Add(Models.GetKeyword(relative.Id));
        }
        return listOfRelatives;
    }
}

Apply this function as follows:

<ul>
    @foreach(var relative in GetRelatedKeyword(Fields.Person)){
         <li>@relative</li>
    }
</ul>

You can also access associated metadata just like you could with the Parent Keyword function. Using this function means you can now get all the juicy bits off of that keyword:

<ul>
    @foreach(var relative in GetRelatedKeyword(Fields.Person)){
        <li>@relative.First_Name @relative.Last_Name</li>
    }
</ul>

Get the Value of a Keyword

As it would turn out, there is no ‘value’ property in the Keyword class. Even though you see “Value” as a field when creating the Keyword, this is actually the title property. So if you’re trying to get the “Value”, your best bet is to try this:

@someKeywordField.Title

If you want something that’ll be more semantic, you can try this out:

@functions{
    public dynamic GetKeywordValue(dynamic kw){
        var Value = kw.TridionObject.Title;
        return Value;
    }
}

And use this like so:

@getKeywordValue(Fields.SomeKeywordField)