Smarty
Smarty
Smarty 2.0 Templates.
class SmartyImplements the Smarty 2.0 template specification, providing parsing, rendering, and management of template content, files, functions, and filters. Supports custom functions, function handlers, post-processing filters, and file-based or string-based templates.
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
Prefix used for generated Smarty template class names.
public string $templatePrefix = '_smarty_template_'Type: string
sourceFile
public File $sourceFileType: Hazaar\File
allowGlobals
Whether global variables are allowed in the template.
public bool $allowGlobals = trueType: bool
functions
List of custom functions available to the template.
public array $functionsType: array
compiler
The compiler instance used for compiling Smarty templates.
public Compiler $compilerType: Hazaar\Template\Smarty\Compiler
customFunctions
List of custom functions registered for the template.
protected array $customFunctionsType: array
content
public string $contentType: string
includes
protected array $includesType: array
filters
private array $filtersType: array
functionHandlers
private array $functionHandlersType: array
defaultParams
private array $defaultParamsType: array
Methods
__construct
Create a new Smarty template object.
public __construct(?string $content, ?Compiler $compiler): voidParameters
| Parameter | Type | Description |
|---|---|---|
$content | string | |
$compiler | Hazaar\Template\Smarty\Compiler |
loadFromString
Load the SMARTy template from a supplied string.
public loadFromString(string $content): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$content | string | The template source code |
loadFromFile
Read the template from a file.
public loadFromFile(File $file): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$file | Hazaar\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): voidReturns: 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(Handler $handler): voidReturns: 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 | Hazaar\Template\Smarty\Handler | the handler object to register |
getTemplate
Returns the original un-compiled template.
public getTemplate(): stringReturns: string
getTemplateFile
Retrieves the template file path.
public getTemplateFile(): ?FileReturns: Hazaar\File
prepend
Prepend a string to the existing content.
public prepend(string $string): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string |
append
Append a string to the existing content.
public append(string $string): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string |
addFilter
Add a post-processing filter to the template.
public addFilter(\Closure $filter): voidReturns: 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): stringReturns: 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): stringReturns: 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): stringReturns: 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): stringReturns: 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 $templatePath): stringReturns: 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$templatePath | string |
checkFilesChanged
Check if any of the files have changed sinace the template was last compiled.
private checkFilesChanged(array $watchFiles, int $timestamp): boolReturns: 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 Fri, 04 Sep 2026 23:32:16 +0000