This topic shows how to call .NET Framework's methods in your template code with ComponentSoft Template Engine. For static methods and properties, you don't have to create a new instance of a .NET object before calling them.
The easiest way to demonstrate what goes on is by example:
Template.tpl Copy Code
{%using("System.IO")/}
$art.ArticleId$ {! Prints out object's property !}
$Path.GetDirectoryName("C:\Temp\Temp.tmp")$ {! Calls the static method GetDirectoryName !}
$art.GetFullName()$ {! Calls method GetFullName() in Article class !}
The output will be:
100
C:\Temp
LEO DANG
C# Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using ComponentSoft;
namespace CSharp
{
class ArticleAuthor
{
public string FullName
{
get
{
return "Leo Dang";
}
}
}
class Article
{
public int ArticleId
{
get
{
return 100;
}
}
public ArticleAuthor ArticleAuthor
{
get
{
return new ArticleAuthor();
}
}
public string GetFullName()
{
return this.ArticleAuthor.FullName.ToUpper();
}
}
public class PropertyAndMethodSample
{
[STAThread]
static void Main()
{
TemplateEngine dt = new TemplateEngine();
dt.LoadFromFile("Template.tpl");
Article art = new Article();
dt.SetValue("art", art);
string output = dt.Run();
Console.WriteLine(output);
}
}
}
VB.NET Copy Code
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports ComponentSoft
Module PropertyAndMethodSample
Class ArticleAuthor
Public ReadOnly Property FullName() As String
Get
Return "Leo Dang"
End Get
End Property
End Class
Class Article
Public ReadOnly Property ArticleId() As Integer
Get
Return 100
End Get
End Property
Public ReadOnly Property ArticleAuthor() As ArticleAuthor
Get
Return New ArticleAuthor()
End Get
End Property
Public Function GetFullName() As String
Return Me.ArticleAuthor.FullName.ToUpper()
End Function
End Class
Sub Main()
Dim dt As New TemplateEngine()
dt.LoadFromFile("Template.tpl")
Dim art As New Article()
dt.SetValue("art", art)
Dim output As String = dt.Run()
Console.WriteLine(output)
End Sub
End Module
No comments:
Post a Comment