PHPackages                             haidarvm/simple-php-template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. haidarvm/simple-php-template

ActiveLibrary[Templating &amp; Views](/categories/templating)

haidarvm/simple-php-template
============================

PHP Simple Template Engine

1.0.3(5y ago)08MITPHPPHP &gt;=5.3.0

Since Aug 8Pushed 5y ago1 watchersCompare

[ Source](https://github.com/haidarvm/simple-php-template)[ Packagist](https://packagist.org/packages/haidarvm/simple-php-template)[ RSS](/packages/haidarvm-simple-php-template/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (5)Used By (0)

Simple Template Engine
======================

[](#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
-----

[](#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.

```
//include the loader
require_once 'path/to/loader.php';

$env = new SimpleTemplateEngine\Environment('path/to/templates/directory');
echo $env->render('template.php');
```

You can also pass in an extension that will be appended to all template paths in Environment.

```
$env = new SimpleTemplateEngine\Environment('path/to/templates', '.php');

//will render index.php
echo $env->render('index');
```

You can pass variables to your template via an array:

```
//index.php
echo $env->render('template.php', ['name'=>$value, 'fruit'=>'banana']);
```

You can then access the variable `$fruit` in your template, and its value will be apple.

```
//template.php
My favourite fruit is .
```

The Environment can hold variables shared by all your templates such as helpers. Set variables like this:

```
$env->helper = new Helper();
$env->colour = "green";
```

Now, in your template, you can use your `Helper` object and your `colour` variable.

```
//inside template.php
My favourite colour is .

```

Blocks
------

[](#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()`:

```
