如果要对输出结果进行条件判断,可以在模板中使用<!--$[if ...]-->标签:
Home.cs:
模板Home.htm:
结果:
对应的Html:
Home.cs:
1
[Layout("master")] [View("home")]
2
public class Home : PageController
3
{
4
public class Employee
5
{
6
public Employee(string name, decimal salary) { Name = name; Salary = salary; }
7
8
public string Name;
9
public decimal Salary;
10
}
11
12
public void Run()
13
{
14
List<Employee> employees = new List<Emplyee>();
15
16
employees.Add( new Employee("Mark Jones" , 65000) );
17
employees.Add( new Employee("John Doe" , 244000) );
18
employees.Add( new Employee("Phil Baxter" , 125000) );
19
20
ViewData["Employees"] = employees;
21
}
22
}
23
[Layout("master")] [View("home")]2
public class Home : PageController3
{4
public class Employee5
{6
public Employee(string name, decimal salary) { Name = name; Salary = salary; }7
8
public string Name;9
public decimal Salary;10
}11
12
public void Run()13
{14
List<Employee> employees = new List<Emplyee>();15
16
employees.Add( new Employee("Mark Jones" , 65000) );17
employees.Add( new Employee("John Doe" , 244000) );18
employees.Add( new Employee("Phil Baxter" , 125000) );19
20
ViewData["Employees"] = employees;21
}22
}23

模板Home.htm:
1
<html>
2
<body>
3
<table>
4
<tr><th>Name</th><th>Salary</th></tr><th> </th>
5
<!--$[foreach employee in Employees]-->
6
<tr>
7
<td>$[employee.Name]</td>
8
<td>$[employee.Salary]</td>
9
<td><!--$[if employee.Salary > 200000]-->Rich Guy!<!--$[endif]--></td></tr>
10
<!--$[endfor]-->
11
</table>
12
<p>There are $[Employees.Count] employees</p>
13
</body>
14
</html>
15
<html>2
<body>3
<table>4
<tr><th>Name</th><th>Salary</th></tr><th> </th>5
<!--$[foreach employee in Employees]-->6
<tr>7
<td>$[employee.Name]</td>8
<td>$[employee.Salary]</td>9
<td><!--$[if employee.Salary > 200000]-->Rich Guy!<!--$[endif]--></td></tr>10
<!--$[endfor]-->11
</table>12
<p>There are $[Employees.Count] employees</p>13
</body>14
</html>15

结果:
| Name | Salary | |
|---|---|---|
| Mark Jones | 65000 | |
| John Doe | 244000 | Rich Guy! |
| Phil Baxter | 125000 |
There are 3 employees
对应的Html:
1
<html>
2
<head><title>My first ProMesh.NET page</title></head>
3
<body>
4
<table>
5
<tr><th>Name</th><th>Salary</th><th> </th></tr>
6
<tr><td>Mark Jones</td><td>65000</td><td></td></tr>
7
<tr><td>John Doe</td><td>244000</td><td>Rich Guy!</td></tr>
8
<tr><td>Phil Baxter</td><td>125000</td><td></td></tr>
9
</table>
10
<p>There are 3 employees</p>
11
</body>
12
</html>
13
<html>2
<head><title>My first ProMesh.NET page</title></head>3
<body>4
<table>5
<tr><th>Name</th><th>Salary</th><th> </th></tr>6
<tr><td>Mark Jones</td><td>65000</td><td></td></tr>7
<tr><td>John Doe</td><td>244000</td><td>Rich Guy!</td></tr>8
<tr><td>Phil Baxter</td><td>125000</td><td></td></tr>9
</table>10
<p>There are 3 employees</p>11
</body>12
</html>13



浙公网安备 33010602011771号