Div PHP Template Engine

Template engine for PHP, with a powerfull features for programmers and template's designers. This solution are implemented in a single class, in one file, considering the template like an object.

Learn more View on GitHub

Overview

Div is a template engine for PHP that allows the separation of labor between the programmer and the designer. As the designer built templates with some specific tags, the programmer uses the template to replace these tags with the information. Div have a compact, descriptive and adaptable syntax for template's designers.

index.php

<?php
include "div.php";

echo new div("index.tpl", array( 
	"name" => "Peter"
));

index.tpl

Hello {$name}

Output

Hello Peter

Of course, the replacement of tags is a basic functionality. More extensions of the substitutions exist, for example, replace repeatedly N times, replace conditionally, among others.

Loops

[$products]
	Name: {$name}
	Price: ${#price:2#}
[/$products]

Conditions

?$login
	Show login form
@else@
	Show content
$login?

Include

<html>
	<head>
		{% head %}
	</head>
	<body>
		{% body %}
	</body>
</html>

The programmer creates an instance of a class with 2 parameters: the first is the designer's code or name of the file that he built, and the second is the information represented by an array, objects, or a combination of arrays and objects to replace the design's tags. The array's indexes, array's keys and object's properties must correspond with the design's tags.

The designer write in text files and use various types of tags: simple replacements, lists, iterations, conditional parts, separating the design into different files, default replacements, and so on.

Div's parser work with 3 ideas:

The syntaxis

The syntax of Div is compact and adaptable. If the programmer wants to do a cycle, the designer only needs to know a name for that cycle, and if he wants to hide a part of the GUI, the designer is only responsible for tag the part that will be hidden or displayed conditionally. If the designer is a expert in other template engine, he can use a dialect to facilitate their work.

Reasons

Div is developed with the philosophy of the knowledge reused. Of course, Div is released in time of recognized template engines that are widely used. For this reason, Div develop a minimum new knowledge so that the developers can quickly become familiar with this engine and they can understand when, how and why to use it.

The features are added if it is really necessary. That is, if there is a need to add another functionality, we first analyzed whether there is a mechanism to resolve this functionality, and then we publish an article that explains how to implement this mechanism.

The argument to develop Div was obtained from various tests with PHP and we concluded that it is faster replace the portions of string than includes of PHP scripts.

The fact remains, that the replacement of substrings is a fast process but it require more memory. However, this consumption is so small that it is worthwhile the sacrifice.

Div development is to avoid creating a cache system because we believe that it is unnecessary according to their characteristics as an engine. A learning system can be sufficient: it can prevent the repeated processing of the same templates.

Finally, it is known that the most popular engines are composed of more than one file, classes and libraries. Div sought since its inception, the implementation of everything in one class, in a single file. This allows easy adaptation to existing development platforms.