Download the PHP package seanmorris/theme without Composer
On this page you can find all versions of the php package seanmorris/theme. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download seanmorris/theme
More information about seanmorris/theme
Files in seanmorris/theme
Package theme
Short Description Simple, sane templating and theming for PHP
License Apache-2.0
Informations about the package theme
SeanMorris\Theme
Simple, sane templating and theming for PHP
The aim of the Theme library is to provide a start separation of presentation from logic. It has only two responsibilities:
- Mapping objects to Views
- Rendering those Views
Composer
Just run composer require seanmorris/theme
in your project directory.
You can also add "seanmorris/theme": "^1.0.0"
to the require
array in your project's composer.json.
Templating
Coupling the template with the View class is very simple. Simply subclass the provided View and append the template after a call to __halt_compiler();
(WITH THE CLOSING ?>
) like so:
(note: Short tags are enabled for simple echo statements as of PHP 5.4, but are not required)
Pass an associative array into the constructor to populate the variables in the template. The keys of the array will be translated to variable names.
Preprocessing
Preprocessing templates is simple. Just implement the method in your view class, and you'll get a chance to operate on the variables prior to rendering.
Usage:
Theming
Creating a theme is as simple as extending the theme class and providing a mapping from your object classes to their view classes, as shown:
Usage:
Wrapping
If you've got a default "trim" you'd like to use to wrap everything (i.e. the view that contains your
, and structure), simply set the static property $wrap to an array listing your wrappers, innermost to outtermost.Usage:
Advanced Stuff...
Although the library doesn't do much, its got some power under the hood.
Fallback Themes
If a theme cannot render an object, it can defer the rendering to other themes that can. This is done by specifying the $themes static property. The list will be check in order, until a theme is able to render a given object.
Subclassing Views
You can subclass any view class and keep the template by ommitting the call to __halt_compiler(), and extending the existing view. You'll probably want to override the parent preprocessor as well.
Contextualized Themeing
By defining mappings from classes to views in the $contextViews array, you can specify that a view should be rendered differently when the render call is made from certain classes.
In this example SeanMorris\Stuff\Foo
will be rendered with the SeanMorris\Theme\FooAlternateView
class when rendered inside of the SeanMorris\Stuff\RandomObject
class, but outside, it will be rendered with SeanMorris\Theme\FooView
.