Templating Base Project

The project (download link below) provides a rich starting functionality for developers about to build templates in .NET and in Visual Studio.

The project can be easily adapted for an organization's templating assembly which we will be uploaded to Tridion and then provide templates to be combined in Page and Component Templates using the Template Builder tool.

The important part of the project is the included TemplateBase.cs class. This class contains many useful methods which are needed throughout the devlopment of Tridion templates.

Methods and Properties

 

Listing all of these methods will be quite an extensive exercise but here are details of some of the more common ones:

NSManager (Property)
An XmlNameSpaceManager already initialized with several XML namespaces such as: tcm, xlink and xhtml

GetComponent (Method)
Returns the component object that is defined in the package for the template

GetPage (Method)
Returns the page object that is defined in the package for the template

GetListStructureGroups  (Method)
Returns the list of structure groups of the current publication

GetStringItemValue  (Method)
Returns the string value for an item in the package with the specified name

And many, many more!

How to start using

 

Notice that the TemplateBase class must be included in the project you upload into Tridion as your templating assembly, otherwise you will see errors about not being able to find the class.

To use the base functionality all one needs to do is inherit from the base class instead of extending the ITemplate interface in the TOM.NET library.

Here is a short code example and the result in the Template Builder:

using System;
using System.Collections.Generic; 
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;
using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager;
using Tridion.ContentManager.Publishing;
using Tridion.Extensions.ContentManager.Templating;

namespace SampleTemplates
{
  class ExtendedTemplate : TemplateBase
  {
   public override void Transform(Engine engine, Package package)
   {
     Initialize(engine, package);
     Logger.Info("this is a logger from a deriving class of the template base class");}
  }
}

Make sure to change the template name for the uploaded assembly in the AssemblyInfo.cs file, in there you can also specify the target folder URI to which the assembly and template classes will be uploaded

Participated in creating this base project

Jeremy Grand-Scrutton (SDLTridion)
Nuno Linhares (SDLTridion)
Yoav Niran (SDLTridion)

Thanks also to:
Will Price (SDLTridion) and Peter Jan Beemster (SDLTridion) for their input and assistance.

Version 1.2 - Updated 09/06/2009 

This new version of the base project includes many new features together with some bug fixing.

Some of the new additions:

  • Added even more utility and helper methods such as: IsPage, IsComponent, GetFieldType, etc.
  • Added support for the new version of the XSLT Mediator: to easily load extension objects from a Tridion TBB.
  • Added a generic class for loading and using XML lists from Tridion (GetListItems)
  • Added the Expression Item Retriever  which is a new and useful way of accessing field values and items in Tridion through an easy to learn expression syntax. (see the 'Expression Item Retrieval.doc' in the zip for more information).

    Example of such an expression: ::content/paragraphs[1]/title� 
    which will retrieve the 'title' embedded field in the second 'paragraph' field of a component.  

Version 1.3 - Update 09/07/2009

This new version of the base project includes a new class called "ItemFieldWrapper", this class wraps any of the ItemField implementations available in the .NET API and expose the values of those as string values.

Since most usage of component field values is to output them to a page as strings, accessing these values becomes cumbersome and annoying since the API doesnt support getting the values of Date, Number or Keyword (to name a few) fields as strings and so an explicit cast must be carried whenever we wish to access these values.

The ItemFieldWrapper takes care of these casting for us and gives us a single point of access to the values of any of the field types available.


Developer
:    Yoav Niran
Company
License agreement
Download package
Tags
:    Templating,   .NET,  Template Builder,  Modular Templates
Version
:   1.3
Source
:   Yes

About the Author
Yoav Niran
Senior Technical Consultant

Yoav joined Tridion in 2006 and is working in the Professional Services Maidenhead office as a senior technical consultant. 

He has been involved in numerous implementations, integrations and development of packaged services.

Yoav has much experience in .NET technologies and ASP.NET web development in particular.

He has also been the technical lead on the reconstruction of the SDL Tridion World website.

For more technical articles from Yoav go to: http://yoavniran.wordpress.com

SDL CMT division

Most Popular Building Blocks

These are the most popular Building Blocks. Did you already get them?

Related building blocks

See these eXtensions related to the Templating Base Project