这是另一个模板引擎:UltTemplate Engine
这是模板代码:
Dear $User.FullName$,
{%set orders=User.GetOrders() /}
Thank you for your order of $orders.Length$ items, We believe you will be very satisfied with the quality of costume pieces included in each. It is this quality that makes our imaginative play apparel so unique.
We created an account for you to track your orders. Here is the login information:
Email: $User.EmailAddress$
Password: $User.Password$
Following is the details of your order (OrderId: $OrderId$):
# Part ID Name Quantity Price(per unit) Sub Total
{%set Total=0.0 /}{%foreach orderproduct,i in orders%}{%set Total = Total + orderproduct.Price * orderproduct.Quantity/}
{%rendertemplate orderproducttemplate item=orderproduct/}{$foreach%}
Total: $PadLeft(Format(Total,"$$#.##"),11)$
If you have any concern, please call us at 913-555-0115.
Sincerely,
$CompanyName$
{%template orderproducttemplate%}$PadLeft(i,4)$$PadLeft(item.PartId, 7)$ $PadRight(item.ProductName, 15)$ $PadRight(item.Quantity, 5)$ $PadLeft(Format(item.Price,"$$#.##"), 7)$ $PadLeft(Format(item.Price*item.Quantity,"$$#.##"), 12)${$template%}
这是输出:
Dear John Borders,
Thank you for your order of 3 items, We believe you will be very satisfied with the quality of costume pieces included in each. It is this quality that makes our imaginative play apparel so unique.
We created an account for you to track your orders. Here is the login information:
Email: myemail@somedomain.com
Password: 123abc
Following is the details of your order (OrderId: 1625DGHJ):
# Part ID Name Quantity Price(per unit) Sub Total
0 1239 Product A 3 $104.09 $312.27
1 22 Product B 1 $134.09 $134.09
2 167 Product C 5 $14.7 $73.5
Total: $519.86
If you have any concern, please call us at 913-555-0115.
Sincerely,
My Company Name
这是 C# 代码:
class OrderProduct
{
private int _partId;
private string _productName;
private int _quantity;
private float _price;
public int PartId
{
get { return _partId; }
set { _partId = value; }
}
public string ProductName
{
get { return _productName; }
set { _productName = value; }
}
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
public float Price
{
get { return _price; }
set { _price = value; }
}
}
class User
{
private string _fullName;
private string _emailAddress;
private string _password;
public string FullName
{
get { return _fullName; }
set { _fullName = value; }
}
public string EmailAddress
{
get { return _emailAddress; }
set { _emailAddress = value; }
}
public string Password
{
get { return _password; }
set { _password = value; }
}
public OrderProduct[] GetOrders()
{
OrderProduct[] ops = new OrderProduct[3];
ops[0] = new OrderProduct();
ops[0].PartId = 1239;
ops[0].Price = 104.09f;
ops[0].ProductName = "Product A";
ops[0].Quantity = 3;
ops[1] = new OrderProduct();
ops[1].PartId = 22;
ops[1].Price = 134.09f;
ops[1].ProductName = "Product B";
ops[1].Quantity = 1;
ops[2] = new OrderProduct();
ops[2].PartId = 167;
ops[2].Price = 14.7f;
ops[2].ProductName = "Product C";
ops[2].Quantity = 5;
return ops;
}
}
private void btnRun_Click(object sender, EventArgs e)
{
try
{
dt.LoadFromString(txtSource.Text);
dt.SetValue("CompanyName", "My Company Name");
User u = new User();
u.EmailAddress = "myemail@somedomain.com";
u.FullName = "John Borders";
u.Password = "123abc";
dt.SetValue("User", u);
dt.SetValue("OrderId", "1625DGHJ");
txtOutput.Text = dt.Run();
}
catch (Exception exc)
{
MessageBox.Show("An error occurred: " + exc.Message);
}
}