Macros in Template Engines - Understanding the Concept
Macros in a template engine are essentially functions or methods that are invoked within the template to process or manipulate data. They serve as powerful tools, enabling the dynamic generation of content, formatting of output, and execution of complex logic directly within the template
Definition and Purpose:
- Macro: A predefined function in the template engine that can be called within the template to perform specific tasks, like data formatting, string manipulation, or even complex logical operations.
- Purpose: Macros enhance the flexibility and capability of a template engine, allowing for more advanced, dynamic, and customized rendering of content.
Usage:
Basic Macro Call:
{$html = '<span class="bold">Hello World</span>'}
{htmlChars|$html}
Here, htmlChars is a macro that takes $html as input and processes it.
Predefined Macro Function
public function HtmlChars(string $value): string {
return htmlspecialchars($value);
}
This function is used to safely render HTML by converting special characters to HTML entities.
Language Constants as Macros
- Direct Call: {hello world} outputs "Hello World".
- Via Macro: {lang|hello world} – achieves the same result, demonstrating multiple ways to call macros.
{`hello world`}
{lang|hello world}
Chained Macros
Multiple macros can be chained, where the output of one becomes the input to the next, allowing for complex data processing within a single line.
{func1|func2|func3|$value}
Key Points:
- Macros significantly reduce the complexity of templates by encapsulating commonly used functionality.
- They provide a bridge between the template and the underlying PHP functions, making templates more powerful and flexible.
- Chaining macros enables complex operations to be performed in a concise and readable manner.
- Macros can handle a wide range of tasks, from simple string manipulations to more complex logical operations, enhancing the capabilities of your template engine.
Using of macros in templates simplifies template syntax, promotes code re-usability, and enhances the expressiveness of your templates, making them an indispensable tool in template development.
Macros: Continuously Evolving for Enhanced Functionality
As part of our commitment to providing a robust and dynamic template engine, we continuously work on expanding and refining our macro functionalities. At this stage, we are pleased to offer a diverse range of macros designed to cater to various common requirements in web development. These macros simplify tasks such as data formatting, string manipulation, conditional rendering, and array handling, directly within your templates. Please note that our collection of macros is an ongoing project, and we regularly update our offerings to include new features and improvements. The macros currently available represent the initial stage of this evolving toolset, crafted to enhance the efficiency and capabilities of your templating experience.
| Macro | Usage | Output | |
|---|---|---|---|
| DateFormat |
|
Friday 9th of January 2026 01:19:35 AM | |
| Escape |
|
alert("a") | |
| Trunc |
|
Lorem Ipsu | |
| Upper |
|
TEXT | |
| Lower |
|
text | |
| Title |
|
Text | |
| IfClass |
|
class="active" | |
| IfEval |
|
active | |
| CompareClass |
|
class="are_equal" | |
| Compare |
|
are_equal | |
| Abs |
|
111 | |
| Max |
|
5 | |
| Min |
|
1 | |
| RoundUp |
|
4 | |
| RoundDown |
|
3 | |
| Sort |
|
[ |
|
| KeySort |
|
[ |
|
| Values |
|
[ |
|
| Unique |
|
[ |
|
| Keys |
|
[ |
|
| Search |
|
Key: 41 |
- Installation - Installing Aurora Template
- Usage - Basic Usage of the PHP NLG Template Engine
- Storage-Setting Directories for Templates and Cache
- Templates - Setting Up Input Templates
- Variables - Setting Variables in the System
- Language Constants- Setting Language Constants
- Cache - Managing Cache Creation
- Router - Standalone Mode with Routing System
- Using the PHP Template Engine
- Blocks - Block Based template engine
- Using variables - How to use variables in templates
- Calculations - Isolation and Sharing Variables
- Include and Import - include vs. import in the Template Engine
- Import - Leveraging `import{}` for Standalone Applications with a Routing System
- Resources - Adding Resources to Templates
- Control Structures - `if`, `else if`, `else`
- Ternary Operator - How to approach to ternary operator
- Macros - Macros in Template Engines - Understanding the Concept
- Custom Macros - Creating Custom Macros in Template Engines
- Forms - Creating Forms in the Template Engine
- Custom forms - Customizing Forms in the Template Engine
- Custom PHP Forms - PHP Developer's Guide to Customizing Form Templates in the Template Engine
- Curly Brackets - Handling Curly Brackets in Text Content
- Error Handling - Error Handling in PHP Template Engine
- Tests