1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<
div
class="query-form">
<
table
>
<
tr
>
<
td
>姓名:</
td
>
<
td
><
input
type="text" id="txtQryName" class="text" value="$qryName" /></
td
>
<
td
>地址:</
td
>
<
td
><
input
type="text" id="txtQryAddress" class="text" value="$qryAddress" /></
td
>
<
td
><
input
type="button" id="btnQuery" value="查询" /></
td
>
</
tr
>
</
table
>
</
div
>
<
br
>
<
h2
>Contact List</
h2
>
<
div
class="contact-list">
#foreach($c in $contacts)
#beforeall
<
table
>
<
tr
class="list-header">
<
td
>姓名</
td
>
<
td
>邮箱</
td
>
<
td
>地址</
td
>
<
td
>会员等级</
td
>
</
tr
>
#odd
<
tr
class="list-odd">
#even
<
tr
class="list-even">
#each
<
td
>$c.set_Name("$c.name - W") $c.get_Name()</
td
>
<
td
><
a
href="mailto:$c.EMail">$c.EMail</
a
></
td
>
<
td
>$c.Address</
td
>
<
td
>#if($c.TotalOrderAmt>1000)钻卡会员
#elseif($c.TotalOrderAmt>200)金卡会员
#else Standard#end</
td
>
#after
</
tr
>
#afterall
</
table
>
#nodata
No contacts found!
#end
</
div
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public
class
Contact
{
public
string
Name {
get
;
set
; }
public
string
EMail {
get
;
set
; }
public
string
Address {
get
;
set
; }
public
decimal
TotalOrderAmt {
get
;
set
; }
}
<br>
protected
void
Page_Load(
object
sender, EventArgs e)
{
VelocityEngine engine =
new
VelocityEngine();
ExtendedProperties prop =
new
ExtendedProperties();
prop.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Server.MapPath(
"./"
));
prop.AddProperty(RuntimeConstants.ENCODING_DEFAULT,
"utf-8"
);
engine.Init(prop);
Template template = engine.GetTemplate(
"contact-list.vm"
);
VelocityContext context =
new
VelocityContext();
context.Put(
"contacts"
,
new
List<Contact>()
{
new
Contact() {
Name=
"Richie Liu"
, TotalOrderAmt=180,
Address=
"上海徐汇区钦州路"
},
new
Contact() {
Name=
"Kevin Zhang"
,TotalOrderAmt=0,
Address=
"苏州新区"
},
new
Contact() {
Name=
"Eric Wong"
, TotalOrderAmt=626,
Address=
"厦门海沧"
},
new
Contact() {
Name=
"RicCC"
, TotalOrderAmt=2080,
Address=
"上海徐汇区钦州路"
}
});
context.Put(
"qryName"
,
"Ric"
);
context.Put(
"qryAddress"
, DateTime.Now);
<br>
template.Merge(context,
this
.Response.Output);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<
br
/>
<
h2
>Item List - Array</
h2
>
<
div
>
#foreach($item in $items1)
#each
$item
#between
,
#end
</
div
>
<
br
>
<
br
/>
<
h2
>Item List - List</
h2
>
<
div
>
#foreach($item in $items2)
#each
$item.ToString("yyyy-MM-dd")
#between
,
#end
</
div
>
<
br
>
<
br
/>
<
h2
>Item List - Dictionary</
h2
>
<
div
>
#foreach($item in $items3)
#each
{ $item.Key - $item.Value }
#between
,#end
</
div
>
<
br
>
<
br
/>
<
h2
>Item List - DataTable</
h2
>
<
div
>
#foreach($item in $items4.Rows)
{ $item.Key - $item.Value }
#between
,
#end
</
div
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
context.Put(
"items1"
,
new
string
[] {
"AAA"
,
"BBB"
,
"CCC"
});
context.Put(
"items2"
,
new
List<DateTime> {
new
DateTime(1979, 1, 1),
new
DateTime(2010, 4, 1) });
<br>
IDictionary<
string
,
decimal
> dic =
new
Dictionary<
string
,
decimal
>();
dic.Add(
"AAA"
, 111M);
dic.Add(
"BBB"
, 222M);
dic.Add(
"CCC"
, 333M);
context.Put(
"items3"
, dic);
<br>
DataTable table =
new
DataTable();
table.Columns.Add(
new
DataColumn(
"Key"
,
typeof
(
string
)));
table.Columns.Add(
new
DataColumn(
"Value"
,
typeof
(
int
)));
DataRow row = table.NewRow();
row[
"Key"
] =
"item 1"
;
row[
"Value"
] = 111;
table.Rows.Add(row);
row = table.NewRow();
row[
"Key"
] =
"item 2"
;
row[
"Value"
] = 222;
table.Rows.Add(row);
context.Put(
"items4"
, table);
|