speedwork / view
Speedwork View library
Requires
- php: >=5.6
- speedwork/container: ^1.0
- speedwork/core: ^1.0
- speedwork/filesystem: ^1.0
Requires (Dev)
- aura/view: ~1.2
- doctrine/instantiator: ^1.0
- friendsofphp/php-cs-fixer: ^2.0
- jakub-onderka/php-console-highlighter: ^0.3.2
- jakub-onderka/php-parallel-lint: ^0.9.2
- kriswallsmith/assetic: ^1.3
- league/plates: ~1.0
- monolog/monolog: >=1.0.0
- mustache/mustache: ~2.4
- ocramius/proxy-manager: ^1.0
- phpmd/phpmd: ^2.4
- phpro/grumphp: ^0.9.6
- phpstan/phpstan: ^0.5
- phpunit/php-code-coverage: ^5.2
- phpunit/phpunit: ~6.2
- piwik/device-detector: ^3.8
- satooshi/php-coveralls: 2.*
- sebastian/phpcpd: ^3.0
- sensiolabs/security-checker: ^3.0
- smarty/smarty: ~3.1
- squizlabs/php_codesniffer: ~2.3
- twig/twig: ~1.24
Suggests
- aura/view: ~1.2 for aura engine support
- kriswallsmith/assetic: ^1.3 for combining assets
- league/plates: ~1.0 for plates engine support
- mustache/mustache: ~2.4 for mustache engine support
- piwik/device-detector: ~3.8
- smarty/smarty: ~3.1 for smarty engine support
- twig/twig: ~1.24 for twig engine support
This package is not auto-updated.
Last update: 2018-06-29 18:38:37 UTC
README
The ViewServiceProvider gives engine-agnostic templating capabilities to your Speedwork application.
Installation with Composer
curl -s http://getcomposer.org/installer | php
php composer.phar require speedwork/view
Usage
Just register the service provider and optionally pass in some defaults.
$app->register(new Speedwork\View\ViewServiceProvider(), array( 'view.globals' => array('foo' => 'bar'), 'view.default_engine' => 'mustache' ));
The provider registers the ArrayToViewListener
which intercepts the output from your controllers and wraps it with a View
object. For it to work, you have to return an array of data from your controller function.
Views
Normally you do not need to instantiate any view entities on your own; the listener will convert your controller output. If you wish to do it manually, the syntax is as follows:
$view = $app['engine']->create($template = '/path/to/template', $context = array('foo' => 'bar'));
Views can be rendered by calling the render()
function, or casting to string:
$output = $view->render(); $output = (string) $view;
Again, you should not need to render your views manually since they will be handled by the Response
object.
View Context
The view entity is simply an instance of ArrayObject
, so you can use regular array notation to set the context, along with convenience functions like with()
:
$view['foo'] = 'bar'; $view->with(array('foo' => 'bar'));
To insert into the global context, use share()
:
$view->share(array('foo' => 'bar'));
You can initialize the global context by overriding view.globals
.
Engines
This library does not handle any actual view rendering; that task is delegated to the templating library of your choice. Currently adapters are provided for:
There is a special DelegatingEngine
which acts as a registry for multiple different engines, selecting the appropriate one based on the template file extension. Since Aura.View, Plates and Raw PHP all use the same default file extension (.php), you will need to manually configure the extension mapping as follows:
$app->register(new Speedwork\View\ViewServiceProvider(), array( 'view.default_engine' => 'php', 'view.engines' => array( 'php' => 'view.engine.plates' ) ));
Composite Views
Views can be nested inside another:
$view->nest($app['engine']->create('foobar.html'), 'section');
For a single view, it is equivalent to:
$view['section'] = $app['engine']->create('foobar.html');
However, the difference lies in nesting multiple views in the same location. Doing this will place the child views adjacent to each other rather than overwriting:
$view->nest($app['engine']->create('foobar.html'), 'section'); $view->nest($app['engine']->create('foobar.html'), 'section'); // foobar.html is now repeated twice
What's more, you can mix and match different engines:
$mustacheView = $app['engine']->create('foo.mustache'); $smartyView = $app['engine']->create('bar.tpl')->nest($mustacheView, 'section');
Nested views will inherit the context of their parent views.
Exception Handling
All rendering exceptions are captured and stored in a shared ExceptionBag
.
To access the last thrown exception, or return all of them:
$exception = $app['engine']->getExceptionBag()->pop(); $exceptions = $app['engine']->getExceptionBag()->all();
License
Released under the MIT license. See the LICENSE file for details.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Make your changes
- Run the tests, adding new ones for your own code if necessary (
phpunit
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request