#with : Context Switching Helper
On this page
Summary
Change the data context to a specific object, simplifying property access within the block. Properties can be accessed directly without the object prefix.
Based on: <template> element with context binding and the With component
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 `` helper compiles to the following Scryber template structure:
<template data-bind="">
<!-- Main content with direct property access -->
<template data-visible="false">
<!-- Optional else content if object is null -->
</template>
</template>
For the alias syntax ``, it creates a variable binding that makes the object available under the specified name.
Notes
- Empty/null objects trigger the `` block
- Use `` to reference the entire context object
- Use `` to go up one level
- Use `` to go up two levels
- Alias names must be valid identifiers
- Context switching simplifies deeply nested property access