Default Scripts

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

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

Methods as bindings

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

Many methods have an implicit default format which can be overridden in the ScriptContext'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 ScriptContext {
    Args = {
        [ScriptConstants.DefaultDateFormat] = "yyyy-MM-dd HH:mm:ss"
    }
}.Init();

HTML Encoding

All values of #Script 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

#Script supports the same arithmetic expressions as JavaScript:

Behavior is as you would expect in JavaScript except for Bitwise OR | needs to be in (parens) to avoid it being treated as a template expression separator and no assignment operations are supported, i.e. =, ++, --, |=, etc. The = is treated as an equality operator == so it lets you use SQL-like queries if preferred: a = 1 and b = 2 or c = 3

For those who prefer wordier descriptions, you can use the equivalent built-in methods:

It should be noted when porting C# code that as script methods 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 methods to customize how values are formatted:

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

String functions

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

Text Style

Trimming and Padding

URL handling

Repeating, Ranges and Generation

Spacing

The following methods 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 methods allow you to test for different conditions:

Object Type Tests

Methods to check the type of objects:

Object Conversions

Conversions and transformations between different types:

Conditional Display

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

Content Handling

Control Execution

The end* methods short-circuits the execution of a method and discard any results. They're useful to use in combination with the use* methods 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 methods with side-effects, block methods, 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 methods are the inverse of the end* methods where they continue execution with a new value if the condition is true:

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

Assignment

You can create temporary arguments within a script 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

Member Expressions

JavaScript member expressions are supported except for calling methods on instances as only registered methods can be invoked.

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 methods 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 method when you want to make your C# objects available to the client JavaScript page:

Embedding in JavaScript

You can use the methods 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:

Eval

By default the evalTemplate method renders Templates in a new ScriptContext which can be customized to utilize any additional plugins, methods and blocks available in the configured SharpPagesFeature, or for full access you can use {use:{context:true}} to evaluate any Template content under the same context that evalTemplate is run on.

E.g. you can evaluate dynamic Template Syntax which makes use of the MarkdownScriptPlugin plugin with:

{{ content |> evalTemplate({use:{plugins:'MarkdownScriptPlugin'}}) |> raw }}

This method is used by the /preview.html API Page to create an API that enables live previews.

Iterating

The map, select method and its inverse selectEach are some ways to generate output for each item in a collection:

Although a lot of times it's easier to use the each Script Block to iterate over items:

If you instead need to iterate over a collection to perform side-effects without generating output you can use forEach:

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

Miscellaneous

Use #raw block method or pass to emit a Template Expression without evaluating it:

Useful when you want to emit a client-side Vue method or show examples of #Script Expressions.

of will let you find values of a specified type:

You can use appSetting method to access the value of the ScriptContext'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 scripts available.

made with by ServiceStack