UltimateTemplateEngine allows you to declare and initialize an array like:
Copy Code
{%arr = {1,2,3,"abc"}/}
{! arr is an array containing four items 1,2,3 and "abc" !}
{%foreach item in arr%}
$item$
javascript:void(0)
{$foreach%}
The output would be:
1
2
3
abc
The array created above is a fixed array and you cannot add more element to it.
To create a fixed array without initialization in your template code, you can call NewFixedArray function returning a reference to a newly created fixed array. See the following example for more details:
Copy Code
{%arr = NewFixedArray(typeof("char"), 5)/}
{! arr is a fixed array of five characters !}
If you wish to create a dynamic array for further manipulation, UltimateTemplateEngine provides another easy declaration for dynamic array, as shown below:
Copy Code
{%dynarr = {}/}
{%dynarr.Add("123");dynarr.Add("abc");/}
{! dynarr is a dynamic array containing two elements 123 and abc !}
With the array declared above you can add, remove elements to/from it.
You can also use indexer operator to assign a value to a specific element in the array. The indexer operator is applied for both fixed array and dynamic array:
Copy Code
{%dynarr = {};dynarr.Add(10)/}
{%arr = {1,2,3}/}
$dynarr[0]$ {! Print out the value at index 0 of the dynamic array !}
$arr[0]$ {! Print out the value at index 0 of the fixed array !}
{%dynarr[0] = 1/} {! Assignment !}
{%arr[0] = 11/} {! Assignment !}
ComponentSoft.net News
Sunday, July 18, 2010
Caching in UltimateTemplateEngine
With ComponentSoft Template Engine, you can easily cache your loaded template data to speed up your application performance by using two events in TemplateEngine class, as shown below:
* Uses OnLoadTemplateFromCache for loading cached template data.
* Uses OnSaveTemplateToCache for saving template data to cache.
The Web Sample code-behind below shows how to load/save template data from/to Http Cache.
C# Copy Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ComponentSoft;
public partial class _Default : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer)
{
try
{
TemplateEngine dt = new TemplateEngine();
dt.OnLoadTemplateFromCache += new EventHandler(dt_OnLoadTemplateFromCache);
dt.OnSaveTemplateToCache += new EventHandler(dt_OnSaveTemplateToCache);
dt.LoadFromFile("MyTemplate.tpl");
dt.SetValue("CompanyName", "My Company Name");
dt.SetValue("EmailAddress", "myemail@somedomain.com");
dt.SetValue("FullName", "John Borders");
dt.Run(writer);
}
catch (Exception exc)
{
writer.Write("An error occurred: " + exc.Message);
}
}
void dt_OnSaveTemplateToCache(object sender, TemplateEngineCacheEventArgs e)
{
HttpContext.Current.Cache[e.CacheName] = e.CacheData;
}
void dt_OnLoadTemplateFromCache(object sender, TemplateEngineCacheEventArgs e)
{
e.CacheData = HttpContext.Current.Cache[e.CacheName];
}
}
VB.NET Copy Code
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports ComponentSoft
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
Try
Dim dt As New TemplateEngine()
AddHandler dt.OnLoadTemplateFromCache, AddressOf dt_OnLoadTemplateFromCache
AddHandler dt.OnSaveTemplateToCache, AddressOf dt_OnSaveTemplateToCache
dt.LoadFromFile("MyTemplate.tpl")
dt.SetValue("CompanyName", "My Company Name")
dt.SetValue("EmailAddress", "myemail@somedomain.com")
dt.SetValue("FullName", "John Borders")
dt.Run(writer)
Catch exc As Exception
writer.Write("An error occurred: " + exc.Message)
End Try
End Sub
Private Sub dt_OnSaveTemplateToCache(ByVal sender As Object, ByVal e As TemplateEngineCacheEventArgs)
HttpContext.Current.Cache(e.CacheName) = e.CacheData
End Sub
Private Sub dt_OnLoadTemplateFromCache(ByVal sender As Object, ByVal e As TemplateEngineCacheEventArgs)
e.CacheData = HttpContext.Current.Cache(e.CacheName)
End Sub
End Class
* Uses OnLoadTemplateFromCache for loading cached template data.
* Uses OnSaveTemplateToCache for saving template data to cache.
The Web Sample code-behind below shows how to load/save template data from/to Http Cache.
C# Copy Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ComponentSoft;
public partial class _Default : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer)
{
try
{
TemplateEngine dt = new TemplateEngine();
dt.OnLoadTemplateFromCache += new EventHandler
dt.OnSaveTemplateToCache += new EventHandler
dt.LoadFromFile("MyTemplate.tpl");
dt.SetValue("CompanyName", "My Company Name");
dt.SetValue("EmailAddress", "myemail@somedomain.com");
dt.SetValue("FullName", "John Borders");
dt.Run(writer);
}
catch (Exception exc)
{
writer.Write("An error occurred: " + exc.Message);
}
}
void dt_OnSaveTemplateToCache(object sender, TemplateEngineCacheEventArgs e)
{
HttpContext.Current.Cache[e.CacheName] = e.CacheData;
}
void dt_OnLoadTemplateFromCache(object sender, TemplateEngineCacheEventArgs e)
{
e.CacheData = HttpContext.Current.Cache[e.CacheName];
}
}
VB.NET Copy Code
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports ComponentSoft
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
Try
Dim dt As New TemplateEngine()
AddHandler dt.OnLoadTemplateFromCache, AddressOf dt_OnLoadTemplateFromCache
AddHandler dt.OnSaveTemplateToCache, AddressOf dt_OnSaveTemplateToCache
dt.LoadFromFile("MyTemplate.tpl")
dt.SetValue("CompanyName", "My Company Name")
dt.SetValue("EmailAddress", "myemail@somedomain.com")
dt.SetValue("FullName", "John Borders")
dt.Run(writer)
Catch exc As Exception
writer.Write("An error occurred: " + exc.Message)
End Try
End Sub
Private Sub dt_OnSaveTemplateToCache(ByVal sender As Object, ByVal e As TemplateEngineCacheEventArgs)
HttpContext.Current.Cache(e.CacheName) = e.CacheData
End Sub
Private Sub dt_OnLoadTemplateFromCache(ByVal sender As Object, ByVal e As TemplateEngineCacheEventArgs)
e.CacheData = HttpContext.Current.Cache(e.CacheName)
End Sub
End Class
Declaring variables
This topics shows how to declare and initialize variables in your template code:
Template.tpl Copy Code
{%double db = 10.4/}
$db$
{%double db2 = 10D/}
$db2$
{%db3 = 10.3/} {! db3 is a double !}
This declares a double and assign it to value of 10.4. The output will be
10.4
10
10.3
UltimateTemplateEngine supports the following value types: bool, char, byte, sbyte, double, float, int, long, short, uint, ulong, ushort, string, fixed array, dynamic array, dictionary and .NET object.
C# Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using ComponentSoft;
namespace CSharp
{
public class Sample
{
[STAThread]
static void Main()
{
TemplateEngine dt = new TemplateEngine();
dt.LoadFromFile("Template.tpl");
string output = dt.Run();
Console.WriteLine(output);
}
}
}
VB.NET Copy Code
Imports ComponentSoft
Module Sample
Sub Main()
Dim dt As TemplateEngine = New TemplateEngine()
Dim output As String
dt.LoadFromFile("Template.tpl")
output = dt.Run()
Console.WriteLine(output)
End Sub
End Module
Template.tpl Copy Code
{%double db = 10.4/}
$db$
{%double db2 = 10D/}
$db2$
{%db3 = 10.3/} {! db3 is a double !}
This declares a double and assign it to value of 10.4. The output will be
10.4
10
10.3
UltimateTemplateEngine supports the following value types: bool, char, byte, sbyte, double, float, int, long, short, uint, ulong, ushort, string, fixed array, dynamic array, dictionary and .NET object.
C# Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using ComponentSoft;
namespace CSharp
{
public class Sample
{
[STAThread]
static void Main()
{
TemplateEngine dt = new TemplateEngine();
dt.LoadFromFile("Template.tpl");
string output = dt.Run();
Console.WriteLine(output);
}
}
}
VB.NET Copy Code
Imports ComponentSoft
Module Sample
Sub Main()
Dim dt As TemplateEngine = New TemplateEngine()
Dim output As String
dt.LoadFromFile("Template.tpl")
output = dt.Run()
Console.WriteLine(output)
End Sub
End Module
Getting reference of template in code
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
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
Including an external template file
In some specific cases, you need to include an external template file to the current context. It can be done by using usetemplate keyword and to render a previously loaded template you have to use rendertemplate keyword, as shown in the following example:
ExtTemplate.tpl Copy Code
{%string str = "Second template"/}
Second Template
Template.tpl Copy Code
{%usetemplate myext "ExtTemplate.tpl"/}
First Template
{%rendertemplate myext/}
The output will be:
First Template
Second Template
C# Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using ComponentSoft;
namespace CSharp
{
public class Sample
{
[STAThread]
static void Main()
{
TemplateEngine dt = new TemplateEngine();
dt.LoadFromFile("Template.tpl");
string output = dt.Run();
Console.WriteLine(output);
}
}
}
VB.NET Copy Code
Imports ComponentSoft
Module Sample
Sub Main()
Dim dt As TemplateEngine = New TemplateEngine()
Dim output As String
dt.LoadFromFile("Template.tpl")
output = dt.Run()
Console.WriteLine(output)
End Sub
End Module
ExtTemplate.tpl Copy Code
{%string str = "Second template"/}
Second Template
Template.tpl Copy Code
{%usetemplate myext "ExtTemplate.tpl"/}
First Template
{%rendertemplate myext/}
The output will be:
First Template
Second Template
C# Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using ComponentSoft;
namespace CSharp
{
public class Sample
{
[STAThread]
static void Main()
{
TemplateEngine dt = new TemplateEngine();
dt.LoadFromFile("Template.tpl");
string output = dt.Run();
Console.WriteLine(output);
}
}
}
VB.NET Copy Code
Imports ComponentSoft
Module Sample
Sub Main()
Dim dt As TemplateEngine = New TemplateEngine()
Dim output As String
dt.LoadFromFile("Template.tpl")
output = dt.Run()
Console.WriteLine(output)
End Sub
End Module
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
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
ComponentSoft Template Engine Calling Restriction
UltimateTemplateEngine allows you to restrict the call to specific namespaces, methods and properties, you can allow and restrict the call to namespaces, methods and properties by using wildcard characters (*,?) or extract names as well.
The following example shows how to use RestrictedMethodList, AllowedMethodList, RestrictedPropertyList and AllowedPropertyList of TemplateEngine class.
C# Copy Code
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ComponentSoft;
namespace MethodAndPropertyRestriction
{
public static class MyTestClass
{
public static string TestString
{
get { return "Static String"; }
}
public static string GetStaticString(string s)
{
return string.Format("GetStaticString returns {0}", s);
}
}
public static class ClassRestricted
{
public static string TestString
{
get { return "Test String in a restricted class"; }
}
}
public static class MethodAndProperty
{
public static string TestStringRestricted
{
get { return "Restricted String"; }
}
public static string GetStaticStringRestricted(string s)
{
return string.Format("GetStaticStringRestricted returns {0}", s);
}
public static string StaticStringAllowed
{
get
{
return "StaticStringAllowed";
}
}
}
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
try
{
UltimateTemplateEngine ctl = new UltimateTemplateEngine();
ctl.LoadFromString("$using(\"MethodAndPropertyRestriction,MethodAndPropertyRestriction\")$$MyTestClass.TestString$\r\n$MyTestClass.GetStaticString(\"Test\")$\r\n$ClassRestricted.TestString$\r\n$MethodAndProperty.TestStringRestricted$\r\n$MethodAndProperty.GetStaticStringRestricted("static string")$\r\n$MethodAndProperty.StaticStringAllowed$");
ctl.RestrictedMethodList.Add("*.ClassRestricted.*");
ctl.RestrictedPropertyList.Add("*.ClassRestricted.*");
ctl.RestrictedMethodList.Add("*.MethodAndProperty.GetStaticString*");
ctl.AllowedMethodList.Add("*.MethodAndProperty.GetStaticStringAllowed");
ctl.RestrictedPropertyList.Add("*.TestStringRestricted");
string s = ctl.Run();
MessageBox.Show(s);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
}
The following example shows how to use RestrictedMethodList, AllowedMethodList, RestrictedPropertyList and AllowedPropertyList of TemplateEngine class.
C# Copy Code
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ComponentSoft;
namespace MethodAndPropertyRestriction
{
public static class MyTestClass
{
public static string TestString
{
get { return "Static String"; }
}
public static string GetStaticString(string s)
{
return string.Format("GetStaticString returns {0}", s);
}
}
public static class ClassRestricted
{
public static string TestString
{
get { return "Test String in a restricted class"; }
}
}
public static class MethodAndProperty
{
public static string TestStringRestricted
{
get { return "Restricted String"; }
}
public static string GetStaticStringRestricted(string s)
{
return string.Format("GetStaticStringRestricted returns {0}", s);
}
public static string StaticStringAllowed
{
get
{
return "StaticStringAllowed";
}
}
}
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
try
{
UltimateTemplateEngine ctl = new UltimateTemplateEngine();
ctl.LoadFromString("$using(\"MethodAndPropertyRestriction,MethodAndPropertyRestriction\")$$MyTestClass.TestString$\r\n$MyTestClass.GetStaticString(\"Test\")$\r\n$ClassRestricted.TestString$\r\n$MethodAndProperty.TestStringRestricted$\r\n$MethodAndProperty.GetStaticStringRestricted("static string")$\r\n$MethodAndProperty.StaticStringAllowed$");
ctl.RestrictedMethodList.Add("*.ClassRestricted.*");
ctl.RestrictedPropertyList.Add("*.ClassRestricted.*");
ctl.RestrictedMethodList.Add("*.MethodAndProperty.GetStaticString*");
ctl.AllowedMethodList.Add("*.MethodAndProperty.GetStaticStringAllowed");
ctl.RestrictedPropertyList.Add("*.TestStringRestricted");
string s = ctl.Run();
MessageBox.Show(s);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
}
Subscribe to:
Posts (Atom)