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.
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.
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.
Most implementations however will need something a bit more realistic soon and they work with a two-stepped approach:
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.
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.
if you are using Razor below link may be useful
http://blog.building-blocks.com/razor-templating-sdl-tridion-2011
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.