Quantcast
Channel: String Writer » building blocks
Viewing all articles
Browse latest Browse all 2

Tridion .Net Template Building Blocks (package) – 2

$
0
0

As part of previous post, this will just explain about package object in .Net Templating.

Just to understand what is package in the context of Tridion…During publishing Content Manager provides the publishable contents to Content Distributor. That is the publisher packages all the contents, metadata and instructions for publishing and deploying, in the form of XML. The transport service will send this package to a receiver in the Content Deployer and the Content Deployer will process the content according to the instruction given in the package. (Can refer the process here)

This package can be accessed through .Net templating(TBB) using C# fragments or .Net assemblies. Actually TBB will be executed in the context of given package. In order to customize the contents/instructions, package (typeof Package) object must be edited during templating process. TBB retrieves the relevant items from the package and store the modified contents back into the package object.

Both .Net templating approaches transforms the package object.

Using C# fragments

C# fragments will be a part of Transform(…) in a predefined class so the package variable is accessible by default.

Using .Net Assemblies

A class must inherit ITemplate interface in order to make it as TBB, which will have Transform(…) as part of interface implementation.

public class SampleTemplate : ITemplate
{

   public void Transform(Engine engine, Package package)
   {

   }
}

Execute using Template Builder

TBB can be executed using Template Builder, which is an add-on tool in Tridion. After including TBB for compilation, Default Finish Actions will be included as last TBB. During this process, the package contains only the contents before processing Default Finish Actions; and after Default Finish Actions has been executed, all the publishable contents will be stored in the Output of package object.

package object:

This is a stack type of object and gives access to the contents of the package being processed by the current template (Component or Page). Any ContentType items can be pushed in or popped out from the package.

Package class is defined in Tridion.ContentManager.Templating namespace.

When the package is being processed against Component Template, the package will contain Component Item, through which the Component details can be accessed.

Example,

package.PushItem("Title", package.CreateStringItem(ContentType.Text, package.EvaluateExpression("Component.Title")));

In case of Page Template, the package will contain Page Item.

Example,

package.PushItem("Title", package.CreateStringItem(ContentType.Text, package.EvaluateExpression("Page.Title")));

Finally the Output Item of Component or Page Template must contain the contents to publish.

Package variables

To access a value in the package, the following string format should be used.

[Item name].([Item name]).[value selector]

Example,

To access Component title => Component.Title

To access CreatedBy from metadata => Component.MetaData.CreatedBy

Here’s some predefined/reserved item names in Templating which are constants of Package class.

Names

Details

Example

Component Contains the component currently being processed Component.Title
Components Contains the list of components associated with the page currently being processed
Page Contains the page currently being processed Page.Title
ComponentTemplate Contains the current component template being processed against the Component
Field Contains the Field of the Component that is being processed Component.MetaData.CreatedBy
Output Contains the content to publish, in XML format
TemplateRepeatIndex Used to looping through the collections in a Dreamweaver template <!– TemplateBeginRepeat name=”Fields.locations” –>${RenderComponentField(“Fields.locations”, TemplateRepeatIndex)}<!– TemplateEndRepeat –>

Some frequently used methods in package:

PushItem(…) – Items will be added in the package using this method, LIFO (Last In, First Out) approach. Each item should be defined with a name in order to insert it in the package but multiple items can have same name.

package.PushItem("PageTitle", package.CreateStringItem(ContentType.Text, package.EvaluateExpression("Page.Title")));

GetByType(…) – Retrieves an item based the specific content type

Item item = package.GetByType(ContentType.Component);

GetAllByType(…) – Retrieves all items based on the specific content type

Items items = package.GetAllByType(ContentType.Component);

GetByName(…) – Retrieves an item by its name

package.GetByName(Package.OutputName);

GetValue(…) – Retrieves the value of an item

Component component = engine.GetObject(package.GetValue("Component.ID")) as Component;

Remove(…) – Removes an item from the package

Item componentsItem = package.GetByName(Package.ComponentsName);
package.Remove(componentsItem); //it will remove only the first item if many items used same name

CreateStringItem(…) – creates string type of an item

package.PushItem("PageTitle", package.CreateStringItem(ContentType.Text, “Sample Page")));

CreateHtmlItem(…) – creates html type of an item

String marketUrl = package.EvaluateExpression("Components.Fields.MarketUrl");
package.PushItem("MarketLink", package.CreateHtmlItem(marketUrl));

EvaluateExpression(…) – evaluates an expression (predefined variables or user defined)

package.PushItem(package.CreateStringItem(ContentType.Text, package.EvaluateExpression("3>=3"))); //user defined
package.PushItem("PageTitle", package.CreateStringItem(ContentType.Text, package.EvaluateExpression("Page.Title"))); //predefined variables


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images