1

Is there any link/ blog/ document related to best practices in compound templating (for Tridion 2011 SP1)?

Specificaly, We want to know how to write out Component Templates in Page templates more efficiently, without much hard coding.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Huston Lopes
  • 622
  • 4
  • 17
  • 1
    Please explain in more detail what you are trying to do. Are you using DWT, C# or XSLT etc. Please share any code you have tried or want to replace. – Chris Summers Aug 24 '12 at 09:58
  • We are upgrading from Tridion 5.3 to 2011 SP1. In Tridion 5.3, All our CT's and PT's were in VBScript and now we are in the process of converting them to Compound Templates.(using .NET assemblies and DWT code both) In short, we have to do a fresh Compound templating setup. We want to know what are the efficient methods to do so in Modular templating. For Eg:We can attach a metadata schema to each CT which contains the section where CT will be displayed on page and accordingly write it out in the PT. We want to know more such ways. – Huston Lopes Aug 24 '12 at 11:26
  • 1
    You clearly already know a lot about what you want to accomplish and how you want to accomplish it. Putting the section as a field in the metadata sounds like a great approach. So what is your question about it? – Frank van Puffelen Aug 24 '12 at 12:44
  • 2
    Rather than putting the section as a metadata field, I'd suggest it's better to take advantage of the benefits of compound templating, and create several component templates with the same template building block implementation, but different names indicating their purpose in the page. This is more transparent for the content editors when they look at the component presentations in the page. In other words, specifying the location of the CP is part of the page composition activity, and not part of creating a CT. To change the position, you should change the page, not the component template. – Dominic Cronin Aug 24 '12 at 18:11

3 Answers3

11

Out-of-the-box example

The Default Dreamweaver Page Design (DWT) that Tridion comes with, renders the Component Presentations like this:

<!-- TemplateBeginRepeat name="Components" -->
  <div>@@RenderComponentPresentation()@@</div>
<!-- TemplateEndRepeat -->

That is probably the simplest way you can get it.

Splitting in C#

Most implementations however will need something a bit more realistic soon and they work with a two-stepped approach:

  1. a C# TBB that splits the Component Presentations into multiple arrays
  2. a DWT TBB that then iterates over each of these arrays in turn

The splitting in step 1 is typically done based on the Component Template's name.

var page = (Page)engine.GetObject(package.GetByName(Package.PageName));
var lists = new Dictionary<string, List<ComponentPresentation>>();

foreach (comm.ComponentPresentation cp in page.ComponentPresentations)
{
    var ct = cp.ComponentTemplate.Title.Replace(" ", "");
    if (!lists.ContainsKey(ct))
        lists.Add(ct, new List<ComponentPresentation>());
        lists[ct].Add(new ComponentPresentation(cp.Component.Id,
                                                cp.ComponentTemplate.Id));
    }

foreach (string token in lists.Keys)
{
    var item = package.CreateStringItem(ContentType.ComponentArray, 
                               ComponentPresentationList.ToXml(lists[token]));
    package.PushItem(token + "Components", item);
}

The above C# is a simplified version of an implementation provided by Nuno Linhares

The DWT then iterates over each array in turn, putting it in the right spot in the page HTML:

<div class='main'>
  <!-- TemplateBeginRepeat name="ArticleSummary" -->
    @@RenderComponentPresentation()@@
  <!-- TemplateEndRepeat -->
</div>

<byline>
  <!-- TemplateBeginRepeat name="SeeAlso" -->
    @@RenderComponentPresentation()@@
  <!-- TemplateEndRepeat -->
</byline>

But your case of splitting the Component Presentations based on a metadata field of the Component Template is probably more realistic. There seem to be as many of these partitioning rules as there are implementers at times, and many of them are equally valid.

Dominic gave a great implementation example here: http://code.google.com/p/tridion-practice/wiki/PartitionComponentPresentations. In his case there is a simple descriptor language to determine which partition each Component Presentation goes into.

Filtering in the DWT

If you don't want to write any C# TBB, you can also accomplish the same in just DWT by use of TemplateBeginIf's

<!-- TemplateBeginRepeat name="Components" -->
  <!-- TemplateBeginIf cond="ComponentTemplate.Title=='Promo Content'" -->
    @@RenderComponentPresentation()@@
  <!-- TemplateEndIf -->
<!-- TemplateEndRepeat -->

Although this works perfectly fine, it does lead to having more logic in your DWT. If that is a route you are looking to take, there are template languages available for Tridion that make such a mix of layout and logic more natural such as the Razor Mediator (mentioned by Ram S too) and the XSLT Mediator.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
4

if you are using Razor below link may be useful

http://blog.building-blocks.com/razor-templating-sdl-tridion-2011

Ram Saurabh
  • 1,586
  • 8
  • 27
  • Hi Ram, Thanks, but we are not using Razor templating currently. – Huston Lopes Aug 24 '12 at 11:27
  • It will be great if you can give a bit more detail about the language/approach you are using. Row/Column approach for webpage layout design is also in practice. You can get a brief about it in below link http://www.tridiondeveloper.com/creating-column-layouts-using-sdl-tridion – Ram Saurabh Aug 24 '12 at 11:50
  • The Razor engine is a much easier upgrade from VBScript than using DWTs. It is the standard view engine included with ASP.NET MVC, is easy to find code samples, and the Tridion Razxor Mediaotr is built on top of the official Microsoft Razor engine. I would strongly suggest to install it and give it a shot for 1-2 days. This will be enough time to port 1 of your CTs, PTs, and TBBs. Check the documentation and wiki from google code. – robrtc Aug 29 '12 at 19:13
3

The high level concepts for how to output Component Presentation content on a page are very similar for Compound Templates and old school VBScript templates.

One way to do this is to write a TBB using .NET which takes the all Component Presentations (CPs) on a page, and groups them into distinct Component Presentation collections, which can then be iterated through using your DWT Page Layout for each page region.

The TBB you write to group the CPs can use any criteria you desire for defining the groups. Personally I use metadata on my Component Templates (CT) to tag them for certain page regions, but you could equally use CT or Schema names, or some other value from your Components, or metadata on the Folders that the Components are stored in.

Chris Summers
  • 10,153
  • 1
  • 21
  • 46