Skip to content

Variables

Craig edited this page May 15, 2018 · 10 revisions

The value of a property or the result of a function can be assigned to a variable. This gets added to the model and can then be used further down the template. The syntax for creating a variable declaration and assignment is:

{{var truncatedText = @Truncate("Here is some text to truncate.", 10)}}

{{truncatedText}}

Which would generate:

Here is so

Nested property paths can also be referenced with variables:

{{var today = GetDate()}}

{{today.Day}}

In addition to properties and functions, variables can also be assigned a string literal, number, boolean literal, boolean expression, key value pair, anonymous types or another variable:

{{var pageTitle = "Nettle"}}

{{var vatRate = 20.0}}

{{var available = true}}

{{var expression = (vatRate >= 20 & available)}}

{{var pair = <"Message", $.Message>}}

{{var model = [Name = "Nettle", Id = 1, Pair = <"Message", $.Message>]}}

{{var heading = pageTitle}}

Boolean Expressions

A boolean expression is an expression which evaluates to either true or false. A boolean expression must be wrapped in brackets. Nested expressions are supported through sub-bracketing. See conditions for more information.

Key Value Pairs

A key value pair is the equivalent to a .NET KeyValuePair<object, object> type. The key and value types can be any supported Nettle data types. The syntax for declaring a key value pair variable is {{var pair = <Key, Value>}}.

For example:

{{var pair = <"Message", $.Message>}}

The statement above would declare a key value pair variable with the key "Message" and a value that will be resolved to what ever the value of the property Message contains.

Anonymous Types

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object, they are similar to anonymous types in C#. In Nettle, anonymous types allow for aggregation of properties and variables into a new object with custom property names. This can be useful, for example if you want to render a partial template that expects a different model type which you do not have access to. In this case, a new model could be created using the anonymous types initialiser and passed into the partial.

The syntax for declaring a anonymous type variable is

{{var model = [Property1 = Value1, Property2 = Value2, ...]}}.

For example:

{{var model = [Name = "Nettle", Id = 1, Pair = <"Message", $.Message>]}}

Reassigning Variables

A variable can be reassigned to any other value once it has been declared. The reassigned value type can be different to the original type (such as a string to a boolean). The following example demonstrates reassigning a variable:

{{var example = "Test"}}

{{example}}

{{reassign example = true}}

{{example}}

Which would generate the following output:

Test

True

Variable Adjusters

Numeric variables can be quickly adjusted using the adjuster operands. Nettle current supports increment (++) and decrement (--) operands. For example:

{{var counter = 0}}
{{counter}}

{{counter++}}
{{counter}}

{{counter--}}
{{counter}}

Which would generate the following output:

0

1

0
Clone this wiki locally