#with : Context Switching Helper
Change the data context to a specific object, simplifying property access within the block. Properties can be accessed directly without the object prefix.
On this page
Syntax
With Alias:
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
object |
Object | Yes | The object to set as the current context |
alias |
Identifier | No | Optional variable name for the object |
Special Variables
| Variable | Description |
|---|---|
this / . |
Reference to the current context object |
../ |
Access parent context |
Examples
Basic Context Switching
Data:
doc.Params["model"] = new {
user = new {
name = "John Doe",
email = "john@example.com",
age = 30
}
};
Output:
<h2>John Doe</h2>
<p>Email: john@example.com</p>
<p>Age: 30</p>
With Else Block
Accessing Parent Properties
Using Alias Syntax
Nested With Blocks
Underlying Implementation
The {{#with}} helper compiles to the following Scryber template structure:
<template data-bind="{{object}}">
<!-- Main content with direct property access -->
<template data-visible="false">
<!-- Optional else content if object is null -->
</template>
</template>
For the alias syntax {{#with object as | alias |}}, it creates a variable binding that makes the object available under the specified name.
Notes
- Empty/null objects trigger the
{{else}}block - Use
{{.}}to reference the entire context object - Use
{{../property}}to go up one level - Use
{{../../property}}to go up two levels - Alias names must be valid identifiers
- Context switching simplifies deeply nested property access