Download the PHP package susros/html-template-engine without Composer
On this page you can find all versions of the php package susros/html-template-engine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download susros/html-template-engine
More information about susros/html-template-engine
Files in susros/html-template-engine
Package html-template-engine
Short Description HTMLTemplateEngine is an easy, simple and lightweight templating engine library for PHP.
License MIT
Informations about the package html-template-engine
HTMLTemplateEngine is an easy, simple and lightweight templating engine library for PHP.
Requirement
PHP Version 5.3 or higher.
Installation
HTMLTemplateEngine can be manually downloaded or cloned by using:
HTMLTemplateEngine is also available on Packagist. To install with composer, add this line to yoru :
or just run:
Templates
The extension of template files is either tpl or html. Template files are all organised in different folders called packages. In each package, there can be just template files or folders to organise or categorise template files.
Here is an example of directory structure for templates:
Template Variables
Template variable is the placeholder for the actual value. The default value can be assigned to the variable. If the default value is not declared, an empty string will be used as default value.
Syntax | Description |
---|---|
{$var} | Variable without default value |
{$var : "default value"} | Variable with default value |
Example:
Template Function
Template functions are used to tell what actions does template engine need to do when the template is being executed. There are two functions supported.
dup
'dup' template function is used to tell template engine to duplicate the template after the values are assigned to variables of the template.
Syntax | Description |
---|---|
{#dup : [Template contents] ;} | Duplicate the template contents |
For example:
loop
'loop' template function is used to tell template engine to make an 'n' copies of templates.
Syntax | Description |
---|---|
{#loop(n) : [Template contents] ;} | Make n copies of template contents |
For example:
Usage
First, include the HTMLTemplateEngine class file. HTMLTemplateEngine is using namespace HTMLTemplateEngine.
Before using HTMLTemplateEngine, please make sure to set the directory of template files. As default, it is set to the current directory of the application.
For example:
Then, template files can be passed through HTMLTEmplateEngine object initiator:
You can get the template by calling package name as static method with the syntax:
For example, to get the template1.tpl from cat1 of package1 as shown in the example above in Templates section:
Once the template engine has been initiated, you can now access template variables in template files just like accessing object's variables. Template variables can be assigned as single value or array.
If we look at the example from the Template Variables section above:
The output of template will be:
The variable $name was not assigned. Therefore, default value "World" is used. The variable $msg is assigned with the value "Good Morngin!" and thus it is used. Otherwise, empty string will be used.
As for the template function 'dup', it will automatically be recognised by HTMLTemplateEngine when the template is being executed with the variables values. Since we want to duplicate the template and assign with different values for each duplicated template, we need to use array in this case. For example, by using the dup template above:
The output will be:
Similar to 'dup' template function, HTMLTemplateEngine will automatically recognise the function 'loop'. It will generate the specified number of copies of the template. For example:
The output will be: