Sunday, July 18, 2010

Template Engine Object Property

UltimateTemplateEngine offers a number of flexible ways to access object's properties and class's static properties. You can deeply access properties as easily as you do in .NET Framework.

The following example shows you how to a access public property, static property of Book class and deeply access a property in the Author class:

Template.tpl Copy Code

$Book.StaticId$

ID: $bk.BookId$ - Author: $bk.Author.Name$

Length of the author's Name: $bk.Author.Name.Length$

The output will be:

AABB

ID: 100 - Author: John Borders

12
C# Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using ComponentSoft;
namespace CSharp
{
class Author
{
public string Name
{
get
{
return "John Borders";
}
}
}
class Book
{
public static string StaticId
{
get
{
return "AABB";
}
}
public int BookId
{
get
{
return 100;
}
}
public Author Author
{
get
{
return new Author();
}
}
}
public class PropertySample1
{
[STAThread]
static void Main()
{
TemplateEngine dt = new TemplateEngine();
dt.LoadFromFile("Template.tpl");
Book book = new Book();
dt.SetValue("bk", book);
dt.UsingNamespace("CSharp,Demo");
string output = dt.Run();
Console.WriteLine(output);
}
}
}
VB.NET Copy Code
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports ComponentSoft
Module PropertySample1
Class Author
Public ReadOnly Property Name() As String
Get
Return "John Borders"
End Get
End Property
End Class
Class Book
Public Shared ReadOnly Property StaticId() As String
Get
Return "AABB"
End Get
End Property
Public ReadOnly Property BookId() As Integer
Get
Return 100
End Get
End Property
Public ReadOnly Property Author() As Author
Get
Return New Author()
End Get
End Property
End Class

Sub Main()
Dim dt As New TemplateEngine()
dt.LoadFromFile("Template.tpl")
Dim book As New Book()
dt.SetValue("bk", book)
Dim output As String = dt.Run()
Console.WriteLine(output)
End Sub
End Module

No comments:

Post a Comment