What if you want to show some information depending on a specific condition? This is possible by using the <!--{{if ...}}--> directives in your template.
Again, we will use our original class:
[Layout("master")] [View("home")] public class Home : PageController { public class Employee { public Employee(string name, decimal salary) { Name = name; Salary = salary; } public string Name; public decimal Salary; } public void Run() { List<Employee> employees = new List<Employee>(); employees.Add( new Employee("Mark Jones" , 65000) ); employees.Add( new Employee("John Doe" , 244000) ); employees.Add( new Employee("Phil Baxter" , 125000) ); ViewData["Employees"] = employees; } }
home.htm
<html> <body> <table> <tr><th>Name</th><th>Salary</th></tr><th> </th> <!--{{ foreach employee in Employees }}--> <tr> <td>{{employee.Name}}</td> <td>{{employee.Salary}}</td> <td><!--{{ if employee.Salary > 200000 }}-->Rich Guy!<!--{{ endif }}--></td></tr> <!--{{ endfor }}--> </table> <p>There are {{ Employees.Count }} employees</p> </body> </html>
The resulting HTML output:
| Name | Salary | ||
|---|---|---|---|
| Mark Jones | 65000 | ||
| John Doe | 244000 | Rich Guy! | |
| Phil Baxter | 125000 | ||
There are 3 employees
<html> <head><title>My first Vici MVC page</title></head> <body> <table> <tr><th>Name</th><th>Salary</th><th> </th></tr> <tr><td>Mark Jones</td><td>65000</td><td></td></tr> <tr><td>John Doe</td><td>244000</td><td>Rich Guy!</td></tr> <tr><td>Phil Baxter</td><td>125000</td><td></td></tr> </table> <p>There are 3 employees</p> </body> </html>
>> Next: Part 4