Class: ExpressionCoreTrait
Full name: Clarity\Engine\Tokenizer\ExpressionCoreTrait
Extracted from Clarity\Engine\Tokenizer to keep each file small. See that class for docs.
Public methods
setEscapeContext() ·
public function setEscapeContext(string $context): void
Set the output-escaping context for the next processExpression() call.
Called by the Compiler as it tracks the current position in the template.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$context |
string | - | 'html' |
Return value
- Type:
void
advanceEscapeScan() ·
public function advanceEscapeScan(string $text): void
Advance the script-position state over a template TEXT segment.
The Compiler calls this for every text segment, in source order, with the
RAW text — before the post-tag line-break rule, because trimming a newline
would change the answer. It tracks the <script> boundaries itself rather
than trusting the escaping context, so a segment that both opens and closes
a script cannot leave the two out of step.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$text |
string | - |
Return value
- Type:
void
resetEscapeScan() ·
public function resetEscapeScan(): void
Forget the script-position scan. Called once per compiled body, because the tokenizer is reused across templates.
Return value
- Type:
void
processExpression() ·
public function processExpression(string $expression): string
Convert an output expression, {{ ... }}, to a PHP expression string.
The pipeline (|>) is processed first. The leftmost segment is the
expression, and each following segment is a filter call. Unless the
pipeline contains raw (the marker that disables escaping) or a bare
escape (the marker that applies it explicitly), the result is escaped for
the current context set by setEscapeContext() — see
escapeExpression().
raw and escape are the two halves of one switch. raw leaves the value
alone and cancels the automatic wrapper; escape applies the wrapper's
work itself and cancels it, so the value is escaped exactly once. Neither
has an effect outside output position.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$expression |
string | - | Raw expression from inside {{ ... }}. |
Return value
- Type:
string - Description: PHP expression (no leading
<?=or trailing?>).
processCondition() ·
public function processCondition(string $expression, bool $asObject = false): string
Convert a Clarity expression without pipeline — used for control structure conditions (if, for, set) where auto-escape is meaningless.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$expression |
string | - | Raw Clarity expression. |
$asObject |
bool | false |
Whether a brace literal in this expression is an OBJECT literal. Set only by the object cast'soperand path; every other caller leaves it off, so { … } compiles to a map exactly as before. |
Return value
- Type:
string - Description: PHP expression.
processLvalue() ·
public function processLvalue(string $var): string
Convert a Clarity variable chain to its PHP lvalue equivalent, for the left-hand side of {% set var = ... %}.
Scope-aware by construction. In open mode the render scope is seeded into
locals, so {% set a = … %} compiles to a plain $a = …, and reads of a
use the same variable. In sandbox mode the target is $__c_va['a']. The
choice is made in the chain emitter, so it matches the read path.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$var |
string | - | Clarity variable name (e.g. 'user.name', 'items[0]'). |
Return value
- Type:
string - Description: PHP lvalue (e.g. '$user', or '$__c_va['user']['name']').
convertVarsAndOps() ·
public function convertVarsAndOps(string $expr, bool $asObject = false): string
Convert a Clarity expression (no pipeline) to PHP by:
- Replacing var-chains with $__c_va[...] accesses
- Replacing logical/string operators with PHP equivalents
- Resolving function calls: a registered name, or a PHP function the policy
allows, compiles. Any other name throws a ClarityException at compile
time. Use the
|>filter pipeline for filters.
Strategy: tokenize the expression into atoms (quoted strings, numbers, identifiers/var-chains, operators, punctuation) and process each atom.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$expr |
string | - | |
$asObject |
bool | false |
Whether a brace literal here is an object literal. Propagates to the literals AND to parenthesised groups, but not through [, whose items are values. |
Return value
- Type:
string