Smarty
Smarty
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($templateContent);
$tpl->render(['tagname' => 'Hello, World!']);
Properties
templatePrefix
public string $templatePrefix = '_smarty_template_'
sourceFile
public string $sourceFile
allowGlobals
public bool $allowGlobals = true
functions
public array $functions
compiler
public Compiler $compiler
customFunctions
protected array $customFunctions
content
protected string $content
includes
protected array $includes
filters
private array $filters
functionHandlers
private array $functionHandlers
Methods
__construct
Create a new Smarty template object.
public __construct(?string $content, ?Compiler $compiler): void
Parameters
Parameter | Type | Description |
---|---|---|
$content | string | |
$compiler | Compiler |
loadFromString
Load the SMARTy template from a supplied string.
public loadFromString(string $content): void
Parameters
Parameter | Type | Description |
---|---|---|
$content | string | The template source code |
loadFromFile
Read the template from a file.
public loadFromFile(File $file): void
Parameters
Parameter | Type | Description |
---|---|---|
$file | File | can be either a Hazaar\File object or a string to a file on disk |
registerFunction
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.
Parameters
Parameter | Type | Description |
---|---|---|
$functionName | string | |
$callback | callable |
registerFunctionHandler
Register a custom function handler.
public registerFunctionHandler(mixed $handler): void
Customer function handlers are objects that can be used to handle custom functions in the template. A custom function is a function that is not built-in and is defined in the template and can be called using the syntax:
{$functionName param1="value" param2="value"}
You can register multiple custom function handlers. The first handler that contains a method with the same name as the function will be used to handle the function.
Parameters
Parameter | Type | Description |
---|---|---|
$handler | mixed |
getTemplate
Returns the original un-compiled template.
public getTemplate(): string
getTemplateFile
Retrieves the template file path.
public getTemplateFile(): ?string
prepend
Prepend a string to the existing content.
public prepend(string $string): void
Parameters
Parameter | Type | Description |
---|---|---|
$string | string |
append
Append a string to the existing content.
public append(string $string): void
Parameters
Parameter | Type | Description |
---|---|---|
$string | string |
addFilter
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.
Parameters
Parameter | Type | Description |
---|---|---|
$filter | \Closure |
render
Renders the stored template with the supplied parameters and return the rendered content.
public render(array $params): string
This method processes the template using the Smarty templating engine, merging default parameters with the provided parameters.
Internally, it calls the renderString
method to handle the rendering process, passing the template content and parameters. The rendered content is then returned as a string.
Parameters
Parameter | Type | Description |
---|---|---|
$params | array | parameters to use when embedding variables in the rendered template |
renderString
Renders a string template with the provided parameters.
public renderString(string $content, array $params): string
This method processes a string template using the Smarty templating engine. It merges default parameters with the provided parameters, prepares the renderer class, and executes the rendering process. Additionally, it applies any registered filters to the rendered content.
Parameters
Parameter | Type | Description |
---|---|---|
$content | string | the string template content to be rendered |
$params | array | An associative array of parameters to be passed to the template. These parameters will be merged with default parameters. Special handling is applied if a key named '*' exists. |
prepareRendererClass
Prepares the renderer class based on the provided content or source file.
private prepareRendererClass(?string $content): string
If content is provided or the source file is not set, it prepares an eval renderer using the given content. Otherwise, it prepares a PHP renderer.
Parameters
Parameter | Type | Description |
---|---|---|
$content | string | Optional content to be rendered. |
prepareEvalRenderer
Prepares and evaluates a renderer for the given template content.
private prepareEvalRenderer(string $content): string
Generates a unique template ID, checks if a class with that ID already exists, and if not, compiles the template content and evaluates the resulting code. Returns the template ID for further use.
Parameters
Parameter | Type | Description |
---|---|---|
$content | string | The template content to be compiled and evaluated. |
preparePHPRenderer
Prepares and returns the PHP renderer class name for the current template.
private preparePHPRenderer(): string
This method generates a unique template class ID based on the source file, checks if the class already exists, and if not, compiles the template source into a PHP file. It watches relevant files for changes to determine if recompilation is necessary, including the source file, the current file, and related compiler and renderer files. If any watched file has changed, or if the compiled template file does not exist, it recompiles the template and writes the generated PHP code to disk. The compiled template file is then included, and the class name is returned.
checkFilesChanged
Check if any of the files have changed sinace the template was last compiled.
private checkFilesChanged(array $watchFiles, int $timestamp): bool
Parameters
Parameter | Type | Description |
---|---|---|
$watchFiles | array | The files to check for changes |
$timestamp | int | The timestamp of the template file |
Generated by Hazaar API Doc Generator on Wed, 17 Sep 2025 00:57:15 +0000