Default Filters

The default filters are a comprehensive suite of safe filters useful within a View Engine or Text Template Generation environment. The source code for all default filters are defined in TemplateDefaultFilters.

For examples of querying with filters checkout the Live LINQ Examples, we'll show examples for other useful filters below:

Filters as bindings

Filters with no arguments can be used in-place of an argument binding, now and utcNow are examples of this:

Many filters have an implicit default format which can be overridden in the TemplateContext's Args, e.g if no Date Format is provided it uses the default format yyyy-MM-dd which can be overridden with:

var context = new TemplateContext {
    Args = {
        [TemplateConstants.DefaultDateFormat] = "yyyy-MM-dd HH:mm:ss"
    }
}.Init();

HTML Encoding

All values of Template Expressions in .html pages are HTML-encoded by default, you can bypass the HTML encoding to emit raw HTML by adding the raw filter as the last filter in an expression:

Arithmetic

There are no operators in Templates, for any arithmetic calculations you can use the built-in filters:

It should be noted when porting C# code that as filters are normal methods they don't follow the implied Order of Operations used in math calculations where an expression like 1 - 2 + 3 * 4 / 5 is executed in the implied order of (1 - 2) + ((3 * 4) / 5). To achieve the same result you'd need to execute them in their implied grouping explicitly:

Math

Most Math APIs are available including pi and e constants:

Date Functions

Formatting

Use format filters to customize how values are formatted:

When no format provided, the default TemplateConstant's formats are used:

String functions

As can be expected from a templating library there's a comprehensive set of string filters available:

Text Style

Trimming and Padding

URL handling

Repeating, Ranges and Generation

Spacing

The following filters can be used to easily control the precise spacing in templates:

The default spacing used for indents \t can be overridden with the Args[DefaultIndent].

Conditional Tests

These filters allow you to test for different conditions:

Object Type Tests

Filters to check the type of objects:

Object Conversions

Conversions and transformations between different types:

Conditional Display

These filters can be used in combination with Conditional Test filters above to control what text is displayed:

Content Handling

Control Execution

The end* filters short-circuits the execution of a filter and discard any results. They're useful to use in combination with the use* filters which discards the old value and creates a new value to be used from that point on in the expression. show is an alias for use that reads better when used at the end of an expression.

You can also use end to discard return values of filters with side-effects, block filters, partials, etc. It also provides an easy way to comment out any expression by prefixing it at the start, e.g: {{ end | unreachable }}

The ifUse and useIf filters are the inverse of the end* filters where they continue execution with a new value if the condition is true:

There's also an only* filter for each end* filter above with the inverse behavior, e.g:

Assignment

You can create temporary arguments within a templates scope or modify existing arguments with:

Let Bindings and Scope Vars

Let bindings allow you to create scoped argument bindings from individual string expressions:

scopeVars lets you create bindings from a Dictionary, List of KeyValuePair's or List of Dictionaries using the key as the name of the argument binding and the value as its value:

Querying Objects

There's also {{ field('Id') }} to retrieve a public Field.

Deep Binding

Whilst you can use string expressions to navigate deep into an objects hierarchy, you can't navigate past where a cast is required. In these cases you need to either to assign a temporary variable or use map, get, let, etc to fetch the instance at which point you can restart traversing the object graph again from that instance's Type. This is needed here because [now] creates a List<object> and it[0].TimeOfDay tries to access TimeOfDay on an object Type.

Mapping and Conversions

Use map when you want to transform each item into a different value:

It's also useful for transforming raw data sources into more manageable ones:

Whilst these filters let you perform some other popular conversions:

Use parseKeyValueText to convert a key/value string into a dictionary, you can then use values and keys to extract the Dictionaries keys or values collection:

Serialization

Use the json filter when you want to make your C# objects available to the client JavaScript page:

Embedding in JavaScript

You can use the filters below to embed data on the client in JavaScript. If it's a valid JS Object or JSON it can be embedded as a native data JS structure without quotes, otherwise use jsString to capture it in a JavaScript string variable:

The JSV Format is great when you want a human-friendly output of an object graph, you can also use dump if you want the JSV output indented:

If needed, the csv and xml serialization formats are also available:

Iterating

The select filter and its inverse forEach are the primary way to generate custom output for each item in a collection:

Use step if you want to iterate using a custom step function:

Miscellaneous

Use pass to emit a Template Expression without evaluating it:

Useful when you want to emit a client-side Vue filter or show examples of Template Expressions.

of will let you find values of a specified type:

You can use appSetting filter to access the value of the TemplateContext's configured AppSettings Provider:

{{ 'websitePublicUrl' | appSetting }} 

Collections and Querying

Checkout the Live LINQ examples to explore the various collection and querying features.

Filter API Reference

See the Filter API Reference for the full list of default filters available.

made with by ServiceStack