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

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

spin/template
=============

An Active Record like pattern for template rendering with PHP - supporting Twig, Smarty, Plates and Dwoo

v1.0.0(10y ago)320MITPHPPHP &gt;=5.4.0

Since Aug 7Pushed 10y ago2 watchersCompare

[ Source](https://github.com/fire015/spin)[ Packagist](https://packagist.org/packages/spin/template)[ Docs](https://github.com/fire015/spin)[ RSS](/packages/spin-template/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

Spin Template
=============

[](#spin-template)

[![Total Downloads](https://camo.githubusercontent.com/ba455950e0c523e3e36cd33e930e5ca70aa405ec7526d6f19e8f1f3bd078764e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f7370696e2f74656d706c6174652e737667)](https://packagist.org/packages/spin/template)[![Build Status](https://camo.githubusercontent.com/b3521ec310d18aac22f980ffa0de1f5dcb42b2888010333183b9b44593ddf1be/68747470733a2f2f7472617669732d63692e6f72672f666972653031352f7370696e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/fire015/spin)

An Active Record like pattern for template rendering with PHP.

- Define and retrieve variables
- Accessor and mutator functions
- Render in any engine
- Built in support for Twig, Smarty, Plates and Dwoo

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

[](#installation)

Install Spin via [composer](http://getcomposer.org/). Then extend `Spin\Template\Template`.

```
composer require spin/template

```

Example
-------

[](#example)

```
class Homepage extends Spin\Template\Template
{
	/**
	 * The template file
	 */
	protected $file = 'templates/home.php';

	/**
	 * Convert the first character of first name to uppercase on retrieval
	 */
    public function getFirstNameAttribute($value)
    {
        return ucfirst($value);
    }

	/**
	 * Convert the whole string to lowercase when setting first name
	 */
    public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = strtolower($value);
    }

	/**
	 * Convert the unix timestamp to a human friendly date on retrieval
	 */
	public function getDateAttribute($value)
	{
		return date('Y-m-d', $value);
	}
}

$template = new Homepage();

// Assign variables as properties
$template->first_name = 'john';
$template->date = time();

// You can also assign variables as an array
$template['weather'] = 'frightful';

// Render the template
echo $template->render();
```

templates/home.php:

```
Hello  today's date is . The weather outside is
```

Output:

```
Hello John today's date is 2015-08-05. The weather outside is frightful.

```

As you can see we assign variables to our class either via properties or array syntax and call the `render()` method, which passes those variables to our template defined in `$file` and return's the rendered template.

#### Accessors and mutators

[](#accessors-and-mutators)

Accessors and mutators allow you to format template variables when retrieving them for rendering or setting their value.

If you have ever used [Eloquent mutators](http://laravel.com/docs/5.1/eloquent-mutators) for getting/setting data on an object it's the same concept (in fact it's almost the same code - thanks Laravel).

To define an accessor, create a `getFooAttribute` method in your class where `Foo` is the camel cased name of the variable you wish to access. See the `getFirstNameAttribute` method in the example above.

To define a mutator, create a `setFooAttribute` method in your class where `Foo` is the camel cased name of the variable you wish to change. See the `setFirstNameAttribute` method in the example above.

Rendering engine
----------------

[](#rendering-engine)

To use a different rendering engine pass an object that implements `Spin\Template\Engine\EngineInterface` to the `setEngine` method.

Support for Twig, Smarty, Plates and Dwoo are already included and can be implemented as per the examples below:

#### Twig

[](#twig)

```
class Homepage extends Spin\Template\Template
{
	protected $file = 'home.twig';

	public function __construct()
	{
		$loader = new Twig_Loader_Filesystem('/path/to/templates');
		$twig = new Twig_Environment($loader);

		$this->setEngine(new Spin\Template\Engine\TwigEngine($twig));
	}
}
```

#### Smarty

[](#smarty)

```
class Homepage extends Spin\Template\Template
{
	protected $file = 'home.smarty';

	public function __construct()
	{
		$smarty = new Smarty();
		$smarty->setTemplateDir('/path/to/templates');
		$smarty->setCompileDir('/path/to/templates_c');

		$this->setEngine(new Spin\Template\Engine\SmartyEngine($smarty));
	}
}
```

#### Plates

[](#plates)

```
class Homepage extends Spin\Template\Template
{
	protected $file = 'home';

	public function __construct()
	{
		$plates = new League\Plates\Engine('/path/to/templates');

		$this->setEngine(new Spin\Template\Engine\PlatesEngine($plates));
	}
}
```

#### Dwoo

[](#dwoo)

```
class Homepage extends Spin\Template\Template
{
	protected $file = 'home.dwoo';

	public function __construct()
	{
		$dwoo = new Dwoo\Core();
		$dwoo->setTemplateDir('/path/to/templates');
		$dwoo->setCompileDir('/path/to/templates_c');

		$this->setEngine(new Spin\Template\Engine\DwooEngine($dwoo));
	}
}
```

About
-----

[](#about)

### Requirements

[](#requirements)

- Spin works with PHP 5.4 or above (excluding the requirements for any template engine you use)

### Submitting bugs and feature requests

[](#submitting-bugs-and-feature-requests)

Bugs and feature request are tracked on [GitHub](https://github.com/fire015/spin/issues).

### License

[](#license)

Spin is licensed under the MIT License - see the `LICENSE.md` file for details.

### Acknowledgements

[](#acknowledgements)

This library is heavily inspired by Laravel's Eloquent model class. Thanks guys!

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3937d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c7f07c2059c02774d30563f1965e460d9b91f6742c890e6b207059c088954f7b?d=identicon)[fire015](/maintainers/fire015)

---

Top Contributors

[![fire015](https://avatars.githubusercontent.com/u/501743?v=4)](https://github.com/fire015 "fire015 (6 commits)")

---

Tags

twigtemplateviewsmartyactiverecordplatesdwoo

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spin-template/health.svg)

```
[![Health](https://phpackages.com/badges/spin-template/health.svg)](https://phpackages.com/packages/spin-template)
```

###  Alternatives

[wyrihaximus/twig-view

Twig powered View for CakePHP

804.7M1](/packages/wyrihaximus-twig-view)[ytake/laravel-smarty

Smarty template engine for Laravel and Lumen

87401.6k](/packages/ytake-laravel-smarty)[text/template

Simple and secure string-template-engine (Twig-like syntax) with nested if/elseif/else, loops, filters. Simple OOP api: Just one class doing the job (2-lines of code). Fast and secure: No code-generation, no eval'ed() code. Extensible by callbacks. Fully tested. Rich examples included.

38201.1k10](/packages/text-template)[latrell/smarty

This package lets you run Smarty3 on Laravel5 elegantly.

127.0k](/packages/latrell-smarty)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
