Sunday, July 18, 2010

Writting a custom function

You can add a custom function to the TemplateEngine class by passing an alias name and function address to SetFunction method. It can led you to replace the Built-in functions by your own functions.

The example below shows how to write two custom functions:

* Pow returns a specified number raised to the specified power.
* Concat returns a concatenated of two specified strings.


C# Copy Code
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ComponentSoft;
namespace Function
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
FunctionSample();
}
static object Power(TemplateEngine dt, object[] args)
{
// Function has two parameters
if (!TemplateEngine.VerifyParameters(dt, "Pow", args, 2))
return string.Empty;
try
{
double x = Convert.ToDouble(args[0]);
double y = Convert.ToDouble(args[1]);
return Math.Pow(x, y);
}
catch (Exception exc)
{
dt.WriteError("Invalid parameter, error: " + exc.Message);
return string.Empty;
}
}
///
/// Although UltimateTemplateEngine already provided a powerful + operator for string object, we purposely create a function to concatenate two strings for demonstration only.
///

static object Concat(TemplateEngine dt, object[] args)
{
// Function has two parameters in string type.
if (!TemplateEngine.VerifyParameters(dt, "Concat", args, 2, typeof(string), typeof(string)))
return string.Empty;
string s1 = args[0] as string;
string s2 = args[1] as string;
return s1 + s2;
}
public static void FunctionSample()
{
try
{
UltimateTemplateEngine dn = new UltimateTemplateEngine();
dn.LoadFromString("Custom Pow Function demo: $x$ ^ $y$ = $Pow(x,y)$\r\nCustom String Concatenation: \"$str1$\" + \"$str2$\" = \"$Concat(str1, str2)$\"");
dn.SetFunction("Pow", new TplRuntimeFunction(Power));
dn.SetFunction("Concat", new TplRuntimeFunction(Concat));

dn.SetValue("x", 15.0f);
dn.SetValue("y", 2.3f);
dn.SetValue("str1", "Hello ");
dn.SetValue("str2", "World");
MessageBox.Show(dn.Run());
}
catch (Exception exc)
{
MessageBox.Show("An exception occurred: " + exc.Message);
}
}
}
}


VB.NET Copy Code
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports ComponentSoft
Module [Function]
Sub Main(ByVal args As String())
FunctionSample()
End Sub
Private Function Power(ByVal dt As UltimateTemplateEngine, ByVal args As Object()) As Object
' Function has two parameters
If Not UltimateTemplateEngine.VerifyParameters(dt, "Pow", args, 2) Then
Return String.Empty
End If
Try
Dim x As Double = Convert.ToDouble(args(0))
Dim y As Double = Convert.ToDouble(args(1))
Return Math.Pow(x, y)
Catch exc As Exception
dt.WriteError("Invalid parameter, error: " + exc.Message)
Return String.Empty
End Try
End Function
'''
''' Although UltimateTemplateEngine already provided a powerful + operator for string object, we purposely create a function to concat two strings for demonstration only.
'''

Private Function Concat(ByVal dt As UltimateTemplateEngine, ByVal args As Object()) As Object
' Function has two parameters in string type.
If Not UltimateTemplateEngine.VerifyParameters(dt, "Concat", args, 2, GetType(String), GetType(String)) Then
Return String.Empty
End If
Dim s1 As String = TryCast(args(0), String)
Dim s2 As String = TryCast(args(1), String)
Return s1 + s2
End Function
Public Sub FunctionSample()
Try
Dim dn As New TemplateEngine()
dn.LoadFromString("Custom Pow Function demo: $x$ ^ $y$ = $Pow(x,y)$" & Chr(13) & "" & Chr(10) & "Custom String Concatenation: ""$str1$"" + ""$str2$"" = ""$Concat(str1, str2)$""")
dn.SetFunction("Pow", New TplRuntimeFunction(AddressOf Power))
dn.SetFunction("Concat", New TplRuntimeFunction(AddressOf Concat))
dn.SetValue("x", 15.0F)
dn.SetValue("y", 2.3F)
dn.SetValue("str1", "Hello ")
dn.SetValue("str2", "World")
MessageBox.Show(dn.Run())
Catch exc As Exception
MessageBox.Show("An exception occurred: " + exc.Message)
End Try
End Sub
End Module

No comments:

Post a Comment