NetLife Guru

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:

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


    {`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:

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

    {$date = date('Y-m-d H:i:s')}
    {DateFormat|$date,'"l jS \of F Y h:i:s A'}
        
Friday 9th of January 2026 01:19:35 AM
Escape

    {Escape|'<script>alert("a")</script>'}
        
alert("a")
Trunc

    {$text = Lorem Ipsum is simply dummy...}
    {Trunc|$text'}
        
Lorem Ipsu
Upper

    {Upper|"text"}
        
TEXT
Lower

    {Lower|"text"}
        
text
Title

    {Title|"text"}
        
Text
IfClass

    {$isLoggedIn = 1}
    {IfClass|$isLoggedIn,active}
        
class="active"
IfEval

    {$isLoggedIn = 1}
    {IfEval|$isLoggedIn,active}
        
active
CompareClass

    {$a = 1}
    {$b = 1}
    {CompareClass|$a,$b,are_equal}
        
class="are_equal"
Compare

    {$a = 1}
    {$b = 1}
    {Compare|$a,$b,are_equal}
        
are_equal
Abs

    {$a = -111}
    {Abs|$a}
        
111
Max

    {$arr = [1,3,4,5]}
    {Max|$arr}
        
5
Min

    {$arr = [1,3,4,5]}
    {Min|$arr}
        
1
RoundUp

    {$num = 3.23}
    {RoundUp|$num}
        
4
RoundDown

    {$num = 3.23}
    {RoundDown|$num}
        
3
Sort

    {$arr = [
        0=>1,
        2=>4,
        3=>4,
        41=>3,
        5=>1,
        434=>3232
    ]}
    {$newArr = sort|$arr}
    {$newArr}
        
[
[0 => 1],
[1 => 1],
[2 => 3],
[3 => 4],
[4 => 4],
[5 => 3232]
]
KeySort

    {$arr = [
        0=>1,
        2=>4,
        3=>4,
        41=>3,
        5=>1,
        434=>3232
    ]}
    {$newArr = KeySort|$arr}
    {$newArr}
        
[
[0 => 1],
[2 => 4],
[3 => 4],
[5 => 1],
[41 => 3],
[434 => 3232]
]
Values

    {$arr = [
        0=>1,
        2=>4,
        3=>4,
        41=>3,
        5=>1,
        434=>3232
    ]}
    {$newArr = Values|$arr}
    {$newArr}
        
[
[0 => 1],
[1 => 4],
[2 => 4],
[3 => 3],
[4 => 1],
[5 => 3232]
]
Unique

    {$arr = [
        0=>1,
        2=>4,
        3=>4,
        41=>3,
        5=>1,
        434=>3232
    ]}
    {$newArr = Unique|$arr}
    {$newArr}
        
[
[0 => 1],
[2 => 4],
[41 => 3],
[434 => 3232]
]
Keys

    {$arr = [
            0=>1,
            2=>4,
            3=>4,
            41=>3,
            5=>1,
            434=>3232
        ]}
    {$newArr = Keys|$arr}
    {$newArr}
        
[
[0 => 0],
[1 => 2],
[2 => 3],
[3 => 41],
[4 => 5],
[5 => 434]
]
Search

    {$arr = [
            0=>1,
            2=>4,
            3=>4,
            41=>3,
            5=>1,
            434=>3232
        ]}
    {$newArr = Search|$arr, 3}
    {$newArr}
        
Key: 41