jensostertag/templify

A simple Templating Engine for PHP

1.0.0 2023-07-15 15:43 UTC

This package is not auto-updated.

Last update: 2024-06-03 11:12:30 UTC


README

Templify is a simple templating engine for PHP. It uses default PHP syntax but handles the logic of displaying, including and fetching the HTML code of template files which makes it very adaptable for any project.

Installation

To install Templify, include it in your project using composer:

{
    "require": {
        "jensostertag/templify": "1.0.0"
    }
}

Usage

Setting the template base directory

Before you can use Templify, you have to specify the base directory where your template files are located. This is done by calling

Templify::setConfig("TEMPLATE_BASE_DIR", __DIR__ . "/templates");

In this example, your template files would have to be located in the 📁 templates/ directory relative to the file where you're setting the base directory. You can also use absolute paths.

Display a template

To display a template (template.php) without binding any variables, call

Templify::display("template.php");

This will display the template file template.php that has to be located in the template base directory you've set earlier. To have a better overview over your file structure, you can also organize your template files in subdirectories and prepend the path to the template file name.

To display a template and bind variables to it, you have to define the variables in an associative array with the keys as variable names and the values as corresponding values:

$variables = [
    "foo" => "bar",
    "bar" => "foo"
];

Then, pass the array to the display() method:

Templify::display("template.php", $variables);

In the template file, you can use them just like any other variable in PHP:

<p><?php echo $foo; ?></p> <!-- bar -->
<p><?php echo $bar; ?></p> <!-- foo -->

Of course, you can define the variable array in the same line as you're passing it to the display() method, this is just for better readability.

Including other templates inside a template file

If you're developing a website where there is a lot of reused code, you can use Templify to include other template files inside a template file. This is done by calling

Templify::include("template.php");

This will include the template file template.php from the 📁 includes/ directory that has to be located in the template base directory you've set earlier (let's say it was 📁 templates/, the above code would include the 📄 templates/includes/template.php template file). To have a better overview over your file structure, you can also organize your template files in subdirectories and prepend the path to the template file name.

Just like with the display() method, you can also bind variables to the included template file:

Templify::include("template.php", ["foo" => "bar"]);
Fetching the HTML code of a template

Templify can also be used to fetch the HTML code of a template file instead of displaying it. This is done by calling

$html = Templify::fetch("template.php", ["foo" => "bar"]);

and comes in handy if you want to send an email with a template as the body.