This week's TBB is Decode Output TBB. This super simple TBB performs a HTML decode on the Output Package item. It then overwrites the Output with the decoded value.
This decoding is useful when outputing the contents of a Text Block field, for example. By default, this field (not RTF) will be stored as encoded HTML, hence the need to decode it before outputing it in the template.
Note: HttpUtility class is part of the System.Web namespace.
This decoding is useful when outputing the contents of a Text Block field, for example. By default, this field (not RTF) will be stored as encoded HTML, hence the need to decode it before outputing it in the template.
Name
|
Decode Output TBB
|
Type
|
· Template in .NET Assembly
|
Description
|
Used to:
· HTML Decode the Item “Output” from Package;
Notes:
This generic TBB expects an Item called “Output” in the
Package.
It decodes its content (performing HTML Decode) and then it
updates the item’s value.
|
Parameters
|
n/a
|
Applicable to
|
Any template where an Item called
‘Output’ exists in the Package
|
The Code
/// <summary>
/// Performs HTML decode on the Output
package item value, then rewrites the Output to the package.
/// </summary>
[TcmTemplateTitle("Decode
Output TBB")]
public class DecodeOutput : ITemplate
{
TemplatingLogger
log = TemplatingLogger.GetLogger(typeof(DecodeOutput));
/// <summary>
/// Main template method.
/// </summary>
/// <param
name="engine">Template render
engine</param>
/// <param
name="package">Template package</param>
public void Transform(Engine
engine, Package package) {
Item
output = package.GetByName("Output");
if
(output == null) {
log.Error("No
output package, do nothing");
return;
}
string
currentOutput = HttpUtility.HtmlDecode(output.GetAsString());
package.Remove(output);
package.PushItem("Output",
package.CreateHtmlItem(currentOutput));
log.Debug("Finished");
}
}
Note: HttpUtility class is part of the System.Web namespace.
Comments