Skip to content

Model Bindings

Craig edited this page Nov 20, 2017 · 11 revisions

Model bindings provide a way of inserting the value of a model property or variable into the content. A string representation of the matched property or variable value will replace the binding code block. The syntax for using a model binding is:

{{Name}}

Nested model binding paths are also supported:

{{User.Identity.Name}}

The dollar sign $ can be used to explicitly denote a property. This is useful when properties are referenced outside of bindings (such as a for loop) where the {{ and }} tags are not supported.

{{$Name}} or {{$.Name}}

A single $ can be used to reference the scopes model. This is useful when inside a for loop where the collection is an array of strings.

{{each $.Names}}
	{{$}}
{{/each}}

The $ binding contains the value of each item as the loop progresses. The code above would generate something like:

Craig
John
Simon

Indexers

Model binding paths support indexers (similar to indexers in C#) for variables or properties that are collections. The index value must be a positive integer that is zero or more. The following example demonstrates using an indexer:

{{$.Names[1]}}

Which given an array Names = ["Craig", "John", "Simon"] would produce the following output:

John

Variables can also be used as index values as long as they resolve to a positive number.

For example:

{{var index = 1}}

{{$.Names[index]}}

Nested indexers are also supported.

For example:

{{$.Names[1].Length}}

Which would return the result 4.

Chained Indexers

If the indexed value returned is also a collection, you can append another indexer to the end of the first to create a chain. Any number of chained indexers are allowed as long as each subsequent return value is a collection.

For example:

{{$.Names[0][0]}}

Which would return the result 'C', as "Craig" is the first item in the array and 'C' is the first character in the string "Craig".

Clone this wiki locally