Download the PHP package haidarvm/simple-php-template without Composer
On this page you can find all versions of the php package haidarvm/simple-php-template. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download haidarvm/simple-php-template
More information about haidarvm/simple-php-template
Files in haidarvm/simple-php-template
Package simple-php-template
Short Description PHP Simple Template Engine
License MIT
Informations about the package simple-php-template
Simple Template Engine
A simple and lightweight PHP templating engine using pure PHP syntax. Simple Template Engine adds on to PHP's templating capabilities by introducing blocks and template inheritance.
It's easy to learn and is useful for small websites or in conjunction with microframeworks.
Requires PHP version 5.3+
Setup
To use the template engine, include loader.php, create an Environment object, and render away!
The Environment's render() function takes the path to a template, renders it and returns its contents as a string.
You can also pass in an extension that will be appended to all template paths in Environment.
You can pass variables to your template via an array:
You can then access the variable $fruit in your template, and its value will be apple.
The Environment can hold variables shared by all your templates such as helpers. Set variables like this:
Now, in your template, you can use your Helper object and your colour variable.
Blocks
Blocks are sections of layout that you can define and then use later.
You can define blocks by enclosing text in $this->block('name here') and $this->endblock():
This will create a Block object that you can access later through $this by using their name.
For example, to output the block defined above:
You can also use if structures to set a default block to use if a block is not defined:
Output Escaping
You can escape blocks of output easily:
The function endblock returns a Block object that you can output.
This is useful for escaping a Block immediately after ending it by calling escape():
As you can see above, we didn't assign a name to our block because we output it right away, and have no need to save it. If you don't assign a name to your block, it cannot be accessed later.
Filters
You can pass a closure to endblock that you want to apply to the content.
The above code converts a block's content to uppercase.
Template Inheritance
Blocks are useful because we can define blocks in one template, then extend another one and use it there!
This allows us to reuse a template such as a layout multiple times with different blocks.
Extending a template is done using the extend function:
When you extend a parent template, any non-block code in the child will become a special block named content in the parent.
In the above code, we defined a title block and some content. Now we can use it in our extended layout.
In layout.php, we can output our content and title with $this['content'] and $this['title'].
Please be careful not to name a block content unless you are intentionally defining a content block. If you define a content block, it will be prepended to the non-block content in the layout.
A template that extends a template that extends a template: If the parent template is extending another template, then any non-block content in the parent will be appended to the content block of their child. This new content block will become the content block of the grandparent.
Templates
Templates can also be created without having to render from file and their blocks can be defined within PHP.
You can create a new template object through the template() function of Environment.
Remember that a Template object created this way must have a layout otherwise nothing will be rendered.
That's all!
Now you have everything you need to create a great website!