-
Notifications
You must be signed in to change notification settings - Fork 6
Template syntax
Vici MVC uses Vici Parser as template rendering engine. Although Vici Parser includes several syntax configuration for the template parser, Vici MVC uses a custom syntax based on the double curly braces syntax.
Any expression that is supposed to output something to the renderd view is written as {{ expression }}. Expressions that are control expressions (like "if", "foreach") don't output anything by themselves, so these expressions are always written as . The reason for this is simple: when viewing HTML templates in designer view, you will only see the things that will actually be rendered in the final rendered view.
Syntax: {{ expression }} or {{ expression ` format }}
The expression can be any C# expression supported by Vici Parser. The expression is evaluated dynamically at runtime, which means that a variable can (in theory) change type every time the expression is evaluated.
The following C# features are supported:
- All arithmic operators
- String concatenation with the "+" operator
- Ternary operator (?:)
- Nullable (coalesce) operator (??)
- Type casting
- Method calling on objects
- Property and field evaluation
In addition, dictionary entries can be accessed using the property accessor syntax (similar to Javascript).
For example, instead of writing:
myDictionary["ItemName"]
you can write:
myDictionary.ItemName
When you try to evaluate a property that doesn't exist, no exception will be thrown, but null will be returned.
Syntax:
<!--{{ if expression }}-->
...
<!--{{ elseif expression }}-->
...
<!--{{ else }}-->
...
<!--{{ endif }}-->
elseif and else are optional
The expression can be any C# expression supported by Vici Parser. Vici MVC will try to convert the resulting expression to a boolean value using the following rules:
Expression return value | Evaluated as |
---|---|
true | true |
false | false |
null | false |
non-empty string | true |
empty string | false |
empty collection (ICollection) | false |
any other object (not null) | true |
Syntax:
<!--{{ foreach var in expression }}-->
...
<!--{{ endfor }}-->
The portion between foreach and endfor is repeated for every element in expression. The current element is stored in var.
The expression must return an object implementing the IEnumerable interface.
Vici MVC does not contain a for(;;) construct (like C#), but there is a way to iterate over a range of numbers using the following syntax:
<!--{{ foreach var in [n1...n2] }}-->
The expression [n1...n2] creates a list of numbers ranging from n1 to n2. Both of these numbers can be any expression, as long as they evaluate to integers (8, 16, 32 or 64 bits).
Inside a loop, the following pseudo-variables are available:
Inside a foreach loop, you can access a few helpful pseudo-variables:
variable name | Replaced by |
---|---|
iteratorName@row | The current row number (starting from 1) |
iteratorName@index | The current row number (starting from 0) |
iteratorName@odd | True for odd rows |
iteratorName@even | True for even rows |
iteratorName@oddeven | "odd" for odd rows, "even" for even rows |
iteratorName@OddEven | "Odd" for odd rows, "Even" for even rows |
iteratorName@ODDEVEN | "ODD" for odd rows, "EVEN" for evan rows |
iteratorName is the variable name you chose as the iterator in the foreach statement.
Syntax:
{{ render templateName , @LocalVar1=expression1, @LocalVar2=expression2 }}
or
<!--{{ render templateName , @LocalVar1=expression1, @LocalVar2=expression2 }}-->
The render statement will include another template and render it using the normal template parser. All ViewData variables are available in the called template, but not temporary variables like the name of the iterator in a {{foreach}} loop. If you want to pass these variables to the sub-template, you should add local variables when calling the template.
The template name is relative to the direcory of the current (calling) template, and should include the extension. The template name can be any expression or a string literal.
You can specify parameters with the render directive. These parameters will be available in the sub-template, along with the global variables (ViewData)
The include statement will include a template without running the the template renderer. The template will be rendered "as is". Only the body part will be included (the part between ''
'' and ''''):{{ INCLUDE templateName }}
or
<!--{{ INCLUDE templateName }}-->
Syntax:
<!--{{ varName = expression }}-->
It's possible to assign values to temporary variables inside a template. You simply create an assignment expression to create a variable. From that point on the variable will be available in your template.
For example, to keep a running total in a table:
<!--{{ runningTotal = 0 }}-->
<table>
<!--{{ foreach item in Items }}-->
<!--{{ runningTotal = runningTotal + item.Price }}-->
<tr><td>{{item.Name}}</td><td>{{item.Price}}</td><td>{{runningTotal}}</td></tr>
<!--{{ endfor }}-->
</table>
In C#, an assignment returns the value of assignment. To prevent this value from being rendered in the view, the "commented" expression tag should be used.