String Writer

my tech stuffs…

Archive for May 2010

Tridion Best Practices for Dreamweaver Templating

leave a comment »

Little about Dreamweaver Templating :)

 

Dreamweaver is a standard WYSIWYG editor for Tridion Templating and is widely used for developing Presentation Templates and Component Templates. These templates will be saved as *.dwt format.

 

Templates can be developed without Dreamweaver editor also. If you do not want to get Dreamweaver license, you can simply type HTML code in Tridion interface, just like in Notepad (as shown below). But the pain point is you won’t get rich editor features.

 

clip_image001

 

Dreamweaver extension is available in sdltridionworld.com.

 

Best Practices

1) In principal, no logic should be created in Dreamweaver templates, only markers

2) Dreamweaver should contain (X)HTML with markers only, leaving it up to C# fragments and/or assembly templates to provide data for the markers

3) Dreamweaver should be used for both PTs and CTs (basically all layouts)

Written by visvabalaji

May 27, 2010 at 12:21 am

Posted in Tridion

Tagged with , ,

Tridion Best Practices for Templating

with 2 comments

Extracted from Tridion manual but modified the code snippets for C# standards.
This topic provides you with implementation tips that are specific to the Tridion Object Model (TOM). Following these guidelines makes it easier to write and maintain your template code.

How to call a COM Component

You can call external COM objects from inside your Template script just as you would inside ASP script. Script extensions enable you to use any COM object. The script extension enables any template to use this function when content is published or previewed. For example:

TDSE tdse = new TDSE();
Object objMyClass= tdse.GetNeweObject("MyCOMComponent.MyClass");
…
objMyClass = null;

Using GetItems and GetListItems

Use the GetItems and GetListItems functions to retrieve items and to create lists of items within an Organizational Item (that is, a Structure Group, Folder, Search Folder, or Category). You can use these functions to build navigation structures or automatic lists.
GetItems gets the contents of an Organizational Item in the form of a collection of TOM objects.

// GetItems retrieves a collection of items
object objStructureGroup = null, objPage = null;
objStructureGroup = TDSE.GetObject("tcm:2-3-4", 1);
// Get all Pages (item type number 64) in this Structure Group
// as a collection of Page objects.
foreach(Page objPage in objStructureGroup.GetItems(64))
{
objPage.Publish("tcm:0-1-65538", false, false, false);
}
objStructureGroup = null;
objPage = null;
GetListItems returns an XML string containing the XML equivalent of the objects returned by GetItems. If you only need the properties of the items, GetListItems gives you much better performance. You can also select which attributes to include or exclude in the XML string, and filter the items returned.
GetListItems is not only a function on all Organizational Items (Structure Group, Folder, Search Folder, or Category) but also on the Publication object. When calling Publication.GetListItems(), you can add the Recursive filter, which retrieves all items in the entire Publication.
The Publication class also has a GetListOrganizationalItems() function, which works like GetListItems() but only returns Organizational Items. Use this function to:
  • Create a report of a Publication’s security structure.
  • Build an organizational item tree.

Using metadata fields

Use metadata fields to specify custom properties for a Page, Structure Group, Folder or Publication. If metadata is added to these items using a Metadata Schema, you can then use the values in these fields.
Metadata values can be used in the following types of scenarios:
  • Localization
  • Archiving
  • Folder ownership
  • Directing publishing processes
  • Automatic publishing
For example, you may want to use a metadata field on one of these items to specify the color of Pages in a specific Structure Group. To retrieve this information from the Page Template, you can then use the MetadataFields object.

objStructureGroup = Page.OrganizationalItem;
WriteOut(objStructureGroup.MetadataFields.Item("Color").Value(1));
For more examples about how you can use metadata fields, refer to the Content Management Implementation Guide.
Using parameters
To pass parameters from the Page Template to the Component Template, use the AddParameter and GetParameter functions.
XSLT natively supports the concept of parameters through xsl:param elements. These parameter declarations can be used on a rule level, but also on a “global” (stylesheet scope) level.
Global XSLT parameters are passed to the XSLT Processor. The ComponentPresentation object internally creates an XSLT Processor for an XSLT Component Template and supports parameters.
In the following example, a Page Template script passes parameters to the (XSLT) Component Templates. The Component Templates retrieve these parameters using GetParameters and use them.

[%
object lComponentPresentation = null, lCount = null;
foreach(ComponentPresentation cp in Page.ComponentPresentations)
{
cp.AddParameter("TestParam1", lCount);
cp.AddParameter("TestParam2", "Test");
WriteOut(cp.Content + "<hr>" + lCount = lCount + 1);
}
%]

Using the Search object

Use the Search object to search for items. You can perform simple and advanced queries. The returned search results contain detailed information about each item found.
The Search object supports the following methods:
  • GetSearchResults returns all search results.
  • GetSearchResultsEx parses the search criteria previously passed to the Component and calls the Verity. Searchcomponent to obtain the search results. After obtaining the basic search results, the function further populates the array with Content Manager-specific information about each item.
  • ReIndexAllPublications rebuilds the full-text search index for all Publications.
  • ReIndexPublications rebuilds the full-text search index for a given Publications.
  • ResultQuery returns the Verity Command Query that was used to obtain the last search results.
  • SetSearchInstruction sets the parameters for search.
In the following example of a Page Template script, the Search object is passed an XML string and returns an XML string. This returned string is then searched and the Title of each item is written to the Page.
[%
#include "tcm:2-237-2048"
Dim objSearch
Dim strSearchString : strSearchString = "GSM OR Mobile"
Dim strSearchXML
Dim strSearchResults
Dim lNumberOfItems
Dim lElement
Dim objXMLResults
objSearch = TDSE.GetSearch
intNumberOfItems = 100
strSearchXML = "<tcm:SearchInstruction"
xmlns:tcm='http://www.tridion.com/ContentManager/5.0'
xmlns:xlink ='http://www.w3.org/1999/xlink'><tcm:SearchQuery"
strSearchXML = strSearchXML & strSearchString
strSearchXML = strSearchXML & "</tcm:SearchQuery> <tcm:SearchIn xlink:href='" & Component.Publication.ID & "' Recursive='1'/>"
</tcm:GeneralParameters><tcm:AdvancedParameters><tcm:NumberOfItems" & intNumberOfItems & "</tcm:NumberOfItems>
</tcm:AdvancedParameters></tcmSearchInstruction>"
Call objSearch.SetSearchInstruction(strSearchXML)
strSearchResults = objSearch.GetSearchResults
objXMLResults = GetNewDOMDocument()
objXMLResults.LoadXML(strSearchResults)
Dim nCount : nCount = 1
For Each lElement In objXMLResults.selectNodes("//tcm:Item")
WriteOut(lElement.getAttribute("Title") & "<BR>")
nCount = nCount + 1
Next
WriteOut(nCount & " results found.")
objXMLResults = Nothing
objSearch = Nothing
%]

Using TDSE

Although it is possible to create a TDSE object by calling a COM Component, the preferred method is to use the global TDSE object.
Use the GetObject method of the TDSE object to access all items in the system. The following examples displays the title of a specific Folder:
[%
Folder objFolder = TDSE.GetObject("tcm:12-25-2", 1);
WriteOut("Folder name: " & objFolder.Title);
objFolder = null;
%]

Written by visvabalaji

May 9, 2010 at 12:12 am

Posted in Tridion

Tagged with , ,

Tridion Best Practices for Coding

leave a comment »

Extracted from Tridion manual but code snippets modified for C# standards.
This topic explains some general coding principles that can help you write and maintain better template code more easily.

Add comments to your Template

Add comments at the top of your Template or Template Building Block. This information may be useful at a later time. For example, here is a comment heading for a Component Template:

//--------------------------------
// Project Name: Argontox
// File Name: Full Component Template
// Written by: Jane Doe
// Description: This Component Template shows all the content of the Component.
// Date: 06/26/2006
// E-mail: jane.doe@website.com
// Version: V2.3
// Revised by: B. Nonymous
// Revised on: 07/03/2006
// Changes made: Fixed bug in GetColorMetadata() function
//--------------------------------

Also, commenting the functionality of your code makes the code easier to read and navigate. For example:


// Functionality:
//--------------------------------
// -For use with Components based on the General_Schema Schema
// -Uses the following fields: headline, subheadline, text, images, links1
// -Links of links1 field are only shown if the link is valid
// -A maximum of 3 links is shown
//--------------------------------

Minimize the use of hard-coded constants and variables

If constants and variables (such as IDs of Components, Pages, Folders, Templates, text or images) are hard-coded in templates, modifying or updating these variables can involve extensive effort.
Where possible, remove hard-coded constants and variables from your Templates, and use references instead. It is possible to store these variables and constants in Template Building Blocks and in metadata fields which can be edited and modified more easily.

Reuse code

Put your code into functions to make your code modular. You can then prevent duplication of code by calling these functions multiple times from your Template scripts. For example:

public void WriteOutCP(object objCP)
{  ...}
// Call the method
WriteOutCP(ComponentPresentation);

To share your code between templates, you can create these functions in Template Building Blocks that can then be used by multiple Templates.

Use <<object>> = null to release memory

To disassociate an object variable from an actual object in VBScript, you can use the Set command to assign Nothing to an object variable.

Set objFolder = Nothing

In C#,


objFolder = null;

Assigning null to an object variable release the memory and system resources associated with the object.

Using variables

Use variables to store often-used object values in. This improves performance. For example, instead of writing:

WriteOut(objComponent.Fields.Item("Heading").Value(1));
WriteOut(objComponent.Fields.Item("Heading").Value(2));

it is better to write the following instead:

string objHeading = objComponent.Fields.Item("Heading");
WriteOut(objHeading.Value(1));
WriteOut(objHeading.Value(2));

Written by visvabalaji

May 5, 2010 at 11:41 pm

Posted in Tridion

Tagged with ,

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: