Smarty 2.0 Templates.
This class implements the entire Smarty 2.0 template specification. For documentation on the Smarty 2.0 template format see the Smarty 2.0 online documentation: https://www.smarty.net/docsv2/en/
Tags are in the format of {$tagname}. This tag would reference a parameter passed to the parser with the array key value of 'tagname'. Such as:
$tpl = new \Hazaar\Template\Smarty($template_content);
$tpl->render(['tagname' => 'Hello, World!']);
public string $ldelim = '{'
public string $rdelim = '}'
public bool $allowGlobals = true
public string $sourceFile
public array $__functions
protected array $__includeFuncs
protected array $__customFunctions
protected array $tags = array (
0 => 'if',
1 => 'elseif',
2 => 'else',
3 => 'section',
4 => 'sectionelse',
5 => 'url',
6 => 'foreach',
7 => 'foreachelse',
8 => 'ldelim',
9 => 'rdelim',
10 => 'capture',
11 => 'assign',
12 => 'include',
13 => NULL,
14 => 'function',
15 => 'call',
16 => 'php',
)
protected string $__content
protected string $__compiledContent
protected array $__includes
private array $__customFunctionHandlers
private array $__sectionStack
private array $__foreachStack
private array $__captureStack
Create a new Smarty template object.
public __construct(?string $content, ?array $customFunctions, ?array $includeFuncs): void
Parameter | Type | Description |
---|
$content | string | |
$customFunctions | array | |
$includeFuncs | array | |
Load the SMARTy template from a supplied string.
public loadFromString(string $content): void
Parameter | Type | Description |
---|
$content | string | The template source code |
Read the template from a file.
public loadFromFile(string $file): void
Parameter | Type | Description |
---|
$file | string | can be either a Hazaar\File object or a string to a file on disk |
public registerFunctionHandler(object $object): void
Parameter | Type | Description |
---|
$object | object | |
Register a custom function with the template.
public registerFunction(string $functionName, callable $callback): void
Custom functions are functions that can be called from within the template. The function must be defined in the template and can be called using the syntax:
{$functionName param1="value" param2="value"}
The function will be called with the parameters as an array. The function must return a string which will be inserted into the template at the point the function was called.
Parameter | Type | Description |
---|
$functionName | string | |
$callback | callable | |
Returns the original un-compiled template.
public getTemplate(): string
Retrieves the template file path.
public getTemplateFile(): ?string
Prepend a string to the existing content.
public prepend(string $string): void
Parameter | Type | Description |
---|
$string | string | |
Append a string to the existing content.
public append(string $string): void
Parameter | Type | Description |
---|
$string | string | |
Add a post-processing filter to the template.
public addFilter(\Closure $filter): void
Filters are applied after the template has been rendered and can be used to modify the output. Useful for things like minifying the output or removing whitespace.
Parameter | Type | Description |
---|
$filter | \Closure | |
Render the template with the supplied parameters and return the rendered content.
public render(array $params = []): string
Parameter | Type | Description |
---|
$params | array | parameters to use when embedding variables in the rendered template |
Set the left and right delimiters for the template.
public setDelimiters(string $ldelim, string $rdelim): void
The default delimiters are '{' and '}'.
Parameter | Type | Description |
---|
$ldelim | string | The left delimiter |
$rdelim | string | The right delimiter |
Compile the template ready for rendering.
This will normally happen automatically when calling Hazaar\Template\Smarty::render() but can be called separately if needed. The compiled template content is returned and can be stored externally.
public compilePHP(): string
public compileENDPHP(): string
protected setType(mixed $value, string $type = 'string', ?string $args): string
Parameter | Type | Description |
---|
$value | mixed | |
$type | string | |
$args | string | |
protected parsePARAMS(string $params, bool $keep_quotes = true): void
Parameter | Type | Description |
---|
$params | string | |
$keep_quotes | bool | |
protected compileVAR(string $name): string
Parameter | Type | Description |
---|
$name | string | |
protected compileVARS(string $string): string
Parameter | Type | Description |
---|
$string | string | |
protected replaceVAR(string $name): string
Parameter | Type | Description |
---|
$name | string | |
protected replaceCONFIG_VAR(string $name): string
Parameter | Type | Description |
---|
$name | string | |
protected compilePARAMS(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileIF(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileELSEIF(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileELSE(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileENDIF(): string
protected compileSECTION(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileSECTIONELSE(): string
protected compileENDSECTION(): string
protected compileURL(string $tag): string
Parameter | Type | Description |
---|
$tag | string | |
protected compileFOREACH(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileFOREACHELSE(): string
protected compileENDFOREACH(): string
protected compileLDELIM(): string
protected compileRDELIM(): string
protected compileCAPTURE(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileENDCAPTURE(): string
protected compileASSIGN(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileFUNCTION(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileENDFUNCTION(): string
protected compileCUSTOMFUNC(string $name, mixed $params): string
Parameter | Type | Description |
---|
$name | string | |
$params | mixed | |
protected compileCUSTOMHANDLERFUNC(object $handler, string $method, mixed $params, int $index): string
Parameter | Type | Description |
---|
$handler | object | |
$method | string | |
$params | mixed | |
$index | int | |
protected compileCALL(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
protected compileINCLUDE(mixed $params): string
Parameter | Type | Description |
---|
$params | mixed | |
Generated by Hazaar API Doc Generator