.NET Page Template Building Block example
The following annotated example explains how you can write a .NET Template Building Block for a Page Template.
For information on setting up a project in Visual Studio with the proper assembly references and the TcmUploadAssembly upload utility, see Creating a .NET assembly Template Building Block.
using System;
// In case you need to retrieve information from your template, you may need to include these namespaces
using Tridion.ContentManager;
using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;
// When writing .Net templates, you will often want to include references to these two namespaces
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;
// The folder where this template should be stored in the CMS can also be set with the
// /folder option on the TcmUploadAssembly tool.
[assembly: TcmTargetFolder("tcm:4-28-2")]
// The name of the assembly template can be set, and defaults to the name of the assembly
[assembly: TcmTemplateTitle("DotNet Assembly")]
namespace Your.Namespace
{
// The name of a template class can be set, and defaults to the name of the class
[TcmTemplateTitle("Page BreadCrumb Sample Template")]
// Templating classes must implement the ITemplate interface
public class PageBreadCrumb : ITemplate
{
// Templating has a logger mechanism that you can use through the
// TemplatingLogger class.
// This logging can be configured in the Content Manager configuration
// file, and is also used in the Output window of the Template Designer.
private TemplatingLogger log =
TemplatingLogger.GetLogger(typeof(PageBreadCrumb));
/// <summary>
/// This is the main transform method, in which perform the actions of your
/// .Net Template Building Block.
/// </summary>
/// <param name="engine">The engine object provide the context for the current
/// publishing operation, as well as access to the CMS</param>
/// <param name="package">The package contains the data related to
/// the current publishing action.</param>
public void Transform(Engine engine, Package package)
{
log.Info("Breadcrumb");
// An active session can be retrieved on the engine
Session session = engine.GetSession();
// The package contains information as well as what is being published
string pageUri = package.GetValue("Page.ID");
// It is possible to retrieve CMS objects in a template
RepositoryLocalObject tridionObject =
(RepositoryLocalObject)session.GetObject(pageUri);
// In this example, the TOM.NET API is used to construct a path to this page.
String breadCrumbString = String.Empty;
while (tridionObject != null)
{
breadCrumbString = " / " + tridionObject.Title + breadCrumbString;
tridionObject = tridionObject.OrganizationalItem;
}
// Besides inputs, information can also be put in the package
// The item named 'Output' will determine the final publishing result
// Here the item is named 'BreadCrumb'. This information item can now
// be used in other templates, for instance in Dreamweaver through
// an expression like @@BreadCrumb@@.
package.PushItem("BreadCrumb", package.CreateHtmlItem(breadCrumbString));
}
}
// It is possible to define multiple templates in one assembly
// Also, it is possible to associate a parameter schema (that must be
// created separately in the Content Manager Explorer GUI) with a template.
[TcmTemplateParameterSchema("tcm:4-271-8")]
public class OtherTemplate : ITemplate
{
public void Transform(Engine engine, Package package)
{
// If the parameter schema associated with this template has
// for example a parameter named 'TemplateParameterName', it
// is temporarily added to the package under that name, and
// its value can be retrieved in a .Net template
string templateParameterValue = package.
GetByName("TemplateParameterName").GetAsString();
// ...
}
}
}