Download the PHP package spin/template without Composer
On this page you can find all versions of the php package spin/template. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spin/template
More information about spin/template
Files in spin/template
Package template
Short Description An Active Record like pattern for template rendering with PHP - supporting Twig, Smarty, Plates and Dwoo
License MIT
Homepage https://github.com/fire015/spin
Informations about the package template
Spin Template
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
Install Spin via composer. Then extend Spin\Template\Template
.
Example
templates/home.php:
Output:
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 allow you to format template variables when retrieving them for rendering or setting their value.
If you have ever used 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
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
Smarty
Plates
Dwoo
About
Requirements
- Spin works with PHP 5.4 or above (excluding the requirements for any template engine you use)
Submitting bugs and feature requests
Bugs and feature request are tracked on GitHub.
License
Spin is licensed under the MIT License - see the LICENSE.md
file for details.
Acknowledgements
This library is heavily inspired by Laravel's Eloquent model class. Thanks guys!