Sunday, July 18, 2010

Introduction to TemplateClass

UltimateTemplateEngine v1.2 introduces the notions of templateclass that allows you to define lots of small templates in a single templateclass, and a template file may contains as many templateclasses as you want. A templateclass can also be defined as an abstract templateclass, so that derived templateclasses should implement abstract methods, as shown in the examples below:
Templateclass file and Template file:
Sample.tpc Copy Code

templateclass thebaseclass
{
themethod() {! Define a base template !}
{#
Oh, great
#}
}

templateclass theabstract
{
templatea(a,b,c); {! Abstract template requiring three parameters !}
templateb(a,b,c); {! Abstract template requiring three parameters !}
templatec(a,b,c)
{#
templatec$a$$b$$c$
#}
}

templateclass theclass : thebaseclass, theabstract {! theclass derives from thebaseclass and theabstract classes !}
{
templatea(a,b,c) {! Override the templatea template !}
{#
$a$$b$$c$
#}

templateb(a,b,c) {! Override the templateb template !}
{#
$a$$b$$c$
#}

mytemplateb(a,b,c) {! Define a new template requiring three parameters !}
{#
$a$$b$$c$

{%template MyEmbeddedTemplate first,second%} {! Defines an embedded template requiring two parameters: "first" and "second"!}

The Embedded Template

{$template%}
#}
}
Template.tpl Copy Code

{! Render template themethod in the "thebaseclass" templateclass !}

{%rendertemplate $theclass.themethod/}



{! Render overriding template templatea in the "theclass", values of 1,2,3 are passed to corresponding parameters !}

{%rendertemplate $theclass.templatea a=1 b=2 c=3/}
Loading template file and templateclass file in C# and VB.NET
C# Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using ComponentSoft;
namespace CSharp
{
public class TemplateClassSample
{
[STAThread]
static void Main()
{
TemplateEngine dt = new TemplateEngine();
dt.LoadTemplateClassesFromFile("Sample.tpc");
dt.LoadFromFile("Template.tpl");
string output = dt.Run();
Console.WriteLine(output);
}
}
}
VB.NET Copy Code
Imports ComponentSoft
Module TemplateClassSample
Sub Main()
Dim dt As TemplateEngine = New TemplateEngine()
Dim output As String
dt.LoadTemplateClassesFromFile("Sample.tpc")
dt.LoadFromFile("Template.tpl")
output = dt.Run()
Console.WriteLine(output)
End Sub
End Module

No comments:

Post a Comment