Compiler for Smarty templates
This class handles the compilation of Smarty template syntax into executable PHP code. It parses Smarty tags, variables, modifiers, and directives and transforms them into their PHP equivalents. The Compiler manages template delimiters, section and foreach loops, variable captures, and includes.
The compilation process follows these main steps:
- Template content is parsed for Smarty syntax using delimiters
- Tags and variables are identified and processed by their respective handlers
- PHP code is generated that, when executed, will render the template output
This class is used internally by the Smarty template engine and should not be instantiated directly by application code.
protected array $tags = array (
0 => 'if',
1 => 'elseif',
2 => 'else',
3 => 'section',
4 => 'sectionelse',
5 => 'foreach',
6 => 'foreachelse',
7 => 'ldelim',
8 => 'rdelim',
9 => 'capture',
10 => 'assign',
11 => 'include',
12 => NULL,
13 => 'function',
14 => 'call',
15 => 'php',
)
Map of conditional operators to PHP operators.
protected array $conditionalMap = array (
'eq' => '==',
'eqeq' => '===',
'ne' => '!=',
'neq' => '!==',
'lt' => '<',
'gt' => '>',
'lte' => '<=',
'gte' => '>=',
'and' => '&&',
'or' => '||',
)
private string $ldelim = '{'
private string $rdelim = '}'
private array $sectionStack
private array $foreachStack
private array $captureStack
private string $compiledContent
public __construct(string $ldelim = '{', string $rdelim = '}'): void
| Parameter | Type | Description |
|---|
$ldelim | string | |
$rdelim | string | |
public setCWD(string $cwd): void
| Parameter | Type | Description |
|---|
$cwd | string | |
public setDelimiters(string $ldelim, string $rdelim): void
| Parameter | Type | Description |
|---|
$ldelim | string | |
$rdelim | string | |
public exec(?string $content): bool
| Parameter | Type | Description |
|---|
$content | string | |
public getCompiledContent(): string
public isCompiled(): bool
public getCode(string $templateObjectId): string
| Parameter | Type | Description |
|---|
$templateObjectId | string | |
public compilePHP(): string
public compileENDPHP(): string
Transforms the provided value into the specified type with optional formatting arguments.
protected setType(mixed $value, string $type = 'string', ?string $args): string
| Parameter | Type | Description |
|---|
$value | mixed | The value to be type converted |
$type | string | The target type to convert to ('date' or 'string') |
$args | string | Optional formatting arguments (e.g., date format string) |
Parses a parameter string into an associative array of parameter values.
protected parsePARAMS(string $params): void
Handles complex parameter strings including arrays, quoted strings, and variable references.
| Parameter | Type | Description |
|---|
$params | string | The parameter string to parse |
Parses a value string that may represent an array or simple value.
protected parseVALUE(string $valueString): mixed
Handles array syntax with brackets and arrow notation.
| Parameter | Type | Description |
|---|
$valueString | string | The string to parse as a value |
Compiles a variable reference into its PHP equivalent.
protected compileVAR(string $name): string
Handles variable modifiers, array access, object properties, and section variables.
| Parameter | Type | Description |
|---|
$name | string | The variable name/reference to compile |
Compiles variables within a string into their PHP equivalents.
protected compileVARS(string $string): string
Replaces all variable references with their compiled versions.
| Parameter | Type | Description |
|---|
$string | string | The string containing variables to compile |
Generates PHP code to write a variable value to output.
protected replaceVAR(string $name): string
| Parameter | Type | Description |
|---|
$name | string | The variable reference to output |
Generates PHP code to write a configuration variable value to output.
protected replaceCONFIGVAR(string $name): string
| Parameter | Type | Description |
|---|
$name | string | The configuration variable name |
Compiles parameters into valid PHP code expressions.
protected compileCONDITIONS(string $params): string
Handles arrays, variables, logical operators, and literal values.
| Parameter | Type | Description |
|---|
$params | string | Parameters to compile |
Recursively compiles an array into its PHP array syntax representation.
protected compileARRAY(array $array): string
| Parameter | Type | Description |
|---|
$array | array | The array to compile |
Compiles an IF statement into its PHP equivalent.
protected compileIF(string $params): string
| Parameter | Type | Description |
|---|
$params | string | The condition expression to evaluate |
Compiles an ELSEIF statement into its PHP equivalent.
protected compileELSEIF(string $params): string
| Parameter | Type | Description |
|---|
$params | string | The condition expression to evaluate |
Compiles an ELSE statement into its PHP equivalent.
protected compileELSE(): string
protected compileENDIF(): string
protected compileSECTION(string $params): string
| Parameter | Type | Description |
|---|
$params | string | |
protected compileSECTIONELSE(): string
protected compileENDSECTION(): string
protected compileFOREACH(string $params): string
| Parameter | Type | Description |
|---|
$params | string | |
protected compileFOREACHELSE(): string
protected compileENDFOREACH(): string
protected compileLDELIM(): string
protected compileRDELIM(): string
protected compileCAPTURE(string $params): string
| Parameter | Type | Description |
|---|
$params | string | |
protected compileENDCAPTURE(): string
protected compileASSIGN(string $params): string
| Parameter | Type | Description |
|---|
$params | string | |
protected compileFUNCTION(string $params): string
| Parameter | Type | Description |
|---|
$params | string | |
protected compileENDFUNCTION(): string
protected compileFUNCTIONHANDLER(string $name, mixed $params): string
| Parameter | Type | Description |
|---|
$name | string | |
$params | mixed | |
protected compileCALL(string $params): string
| Parameter | Type | Description |
|---|
$params | string | |
protected compileINCLUDE(string $params): string
| Parameter | Type | Description |
|---|
$params | string | |
Generated by Hazaar API Doc Generator on Mon, 27 Oct 2025 13:01:29 +0000