PHPackages                             ashley/template-engine - 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. ashley/template-engine

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

ashley/template-engine
======================

Simple Template Engine

v1.0.0(4y ago)08PHP

Since Oct 17Pushed 4y ago1 watchersCompare

[ Source](https://github.com/realashleybailey/simpleTemplateEngine)[ Packagist](https://packagist.org/packages/ashley/template-engine)[ RSS](/packages/ashley-template-engine/feed)WikiDiscussions main Synced today

READMEChangelog (1)DependenciesVersions (2)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+

Installation
------------

[](#installation)

Installation is super-easy via [Composer](https://getcomposer.org/):

```
$ composer require ashley/template-engine
```

or add it by hand to your `composer.json` file.

Setup
-----

[](#setup)

To use the simple template engine, include composer, 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.

```
require_once 'vendor/autoload.php';

$env = new Ashley\TemplateEngine\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 Ashley\TemplateEngine\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()`:

```
