Archive for May 2010
Tridion Best Practices for Dreamweaver Templating
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.
![]()
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)
Tridion Best Practices for Templating
How to call a COM Component
TDSE tdse = new TDSE();
Object objMyClass= tdse.GetNeweObject("MyCOMComponent.MyClass");
…
objMyClass = null;
Using GetItems and GetListItems
// 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;
- Create a report of a Publication’s security structure.
- Build an organizational item tree.
Using metadata fields
- Localization
- Archiving
- Folder ownership
- Directing publishing processes
- Automatic publishing
objStructureGroup = Page.OrganizationalItem;
WriteOut(objStructureGroup.MetadataFields.Item("Color").Value(1));
[%
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
- 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.
[%
#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
[%
Folder objFolder = TDSE.GetObject("tcm:12-25-2", 1);
WriteOut("Folder name: " & objFolder.Title);
objFolder = null;
%]
Tridion Best Practices for Coding
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));
