Accessing Page Template Metadata from a Razor TBB

595 views Asked by At

Does anyone know if it's possible to read Page Template Metadata from within a Razor TBB? I'm implementing a design that's been built using the 960 grid system and to keep my CT's reusable I was hoping to be able to do something like:

<div class="@Page.Template.Metadata.content_grid">
</div>

Unfortunately this throws a null reference exception.

Is what I'm attempting possible or do I need to write a custom TBB to add this information to the package?

2

There are 2 answers

0
richeym On BEST ANSWER

After heading over to Alex's blog to ask the question, I see that he's already answered it for someone else:

Regarding getting Template Metadata out… unfortunately I have not created a wrapper yet for the Template itself, the @Template that is exposed right now is just the Tridion.ContentManager.CommunicationManagement.Template one, which means for now you’ll have to get template fields out the old fashioned way:

@{ Tridion.ContentManager.ContentManagement.Fields.ItemFields templateFields = new ContentManager.ContentManagement.Fields.ItemFields(Template.Metadata, Template.MetadataSchema); }

I’ve added this on my To Do list now though, so you’ll be sure to see a convenience wrapper that will allow you to do @Template.MetaData.YourField in version 1.3.

0
Jonathan Williams On

I was able to get this for Component Template Metadata using the following code:

@foreach (var cp in ComponentPresentations) {
    @if (cp.Template.Metadata != null && cp.Template.Metadata["position"] != null){
        @Debug(string.Format("The Component Template position is {0}", cp.Template.Metadata["position"].InnerText))
        @if (cp.Template.Metadata["position"].InnerText.ToLower().Equals("main")){                              
            @cp.RenderComponentPresentation()
        }
    }
}

Therefore, I think that you should be able to use something similar to the following:

@if (Page.Template.Metadata != null && Page.Template.Metadata["content_grid"] != null){
    @Debug(string.Format("The content_grid value is {0}", Page.Template.Metadata["content_grid"].InnerText))
    <div class="@Page.Template.Metadata["content_grid"].InnerText">
    </div>
}

Please note the .InnerText and not .Value as the content_grid is returned as xml. In my code 'position' was just a string. I guess that this may be different if you're using taxonomy.

Thanks, Jonathan

P.S. I am using version 1.2 of the Razor Mediator