If you wish to obtain a reference to a template object and render it, you have to use the FindTemplate method in the TemplateEngine class of the ComponentSoft TemplateEngine.
There are two kinds of templates you need to consider when finding them:
1. Template inside a templateclass: you need to add '$' character at the beginning of templateclass name and template name; e.g.: to find a template named 'mymethod' in 'myclass' templateclass you need to pass '$myclass.mymethod' to the FindTemplate, that will be: FindTemplate("$myclass.mymethod");
2. Normal template; a template are not inside any templateclasses: you need to pass the template name to the FindTemplate method.
To find a child template you use character '.'(dot) in between parent and child templates.
The following examples demonstrate how to use FindTemplate method to find a template:
Sample.tpc Copy Code
templateclass myclass
{
mytemplate(a,b,c,d)
{#
$a - b - d - c$
{%template child1%}
This is a child template in a class.
{$template%}
#}
}
Template.tpl Copy Code
Hi,
{%template child2%}
This is a child template.
{$template%}
C# Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using ComponentSoft;
namespace CSharp
{
public class TemplateClass2Sample
{
[STAThread]
static void Main()
{
TemplateEngine dt = new TemplateEngine();
string output;
dt.LoadTemplateClassesFromFile("Sample.tpc");
dt.LoadFromFile("Template.tpl");
dt.SetValue("a", 100);
dt.SetValue("b", 2);
dt.SetValue("c", 1);
dt.SetValue("d", 8);
// Find template "mytemplate" inside "myclass" and render it.
Template tpl1 = dt.FindTemplate("global::myclass.mytemplate");
output = dt.Run(tpl1);
Console.WriteLine(output);
// Find child template "child1" of "mytemplate" inside "myclass" and render it.
Template tpl2 = dt.FindTemplate("global::myclass.mytemplate.child1");
output = dt.Run(tpl2);
Console.WriteLine(output);
// Find child template "child2" defined in Template.tpl and render it.
Template tpl3 = dt.FindTemplate("child2");
output = dt.Run(tpl3);
Console.WriteLine(output);
// Render the mainly loaded template.
output = dt.Run();
Console.WriteLine(output);
}
}
}
VB.NET Copy Code
Imports ComponentSoft
Module TemplateClass2Sample
Sub Main()
Dim dt As New TemplateEngine()
Dim output As String
dt.LoadTemplateClassesFromFile("Sample.tpc")
dt.LoadFromFile("Template.tpl")
dt.SetValue("a", 100)
dt.SetValue("b", 2)
dt.SetValue("c", 1)
dt.SetValue("d", 8)
' Find template "mytemplate" inside "myclass" and render it.
Dim tpl1 As Template = dt.FindTemplate("global::myclass.mytemplate")
output = dt.Run(tpl1)
Console.WriteLine(output)
' Find child template "child1" of "mytemplate" inside "myclass" and render it.
Dim tpl2 As Template = dt.FindTemplate("global::myclass.mytemplate.child1")
output = dt.Run(tpl2)
Console.WriteLine(output)
' Find child template "child2" defined in Template.tpl and render it.
Dim tpl3 As Template = dt.FindTemplate("child2")
output = dt.Run(tpl3)
Console.WriteLine(output)
' Render the mainly loaded template.
output = dt.Run()
Console.WriteLine(output)
End Sub
End Module
No comments:
Post a Comment