2017 © Pedro Peláez
 

library lfj-zfrenderer

A utility to render PHP view scripts using the Zend Framework 2 PhpRenderer

image

lorenzoferrarajr/lfj-zfrenderer

A utility to render PHP view scripts using the Zend Framework 2 PhpRenderer

  • Tuesday, December 13, 2016
  • by lorenzoferrarajr
  • Repository
  • 1 Watchers
  • 2 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

lfj-zfrenderer

A library to render PHP view scripts using the Zend Framework 2 PhpRenderer, (*1)

Build Status, (*2)

Warning: This library is in development. Please specify commit hash if you want to experiment with it., (*3)

Installation

The suggested installation method is via composer:, (*4)

composer require lorenzoferrarajr/lfj-zfrenderer

Immutability

An object instantiated from the Lfj\ZfRenderer\Renderer class is immutable., (*5)

For example, the following code is used to add a custom HelperPluginManager instance to the $renderer object, (*6)

$helperPluginManager = new \Zend\View\HelperPluginManager();
$helperPluginManager->setService('name', new PrintName());

$renderer = new \Lfj\ZfRenderer\Renderer();
$rendererNew = $renderer->withHelperPluginManager($helperPluginManager);

Calling $renderer->withHelperPluginManager($helperPluginManager); does not affect the $renderer object. Instead a cloned object of $renderer containing the specified $helperPluginManager is returned., (*7)

Usage

The terms template and view script are used interchangeably., (*8)

Templates are PHP scripts that mix HTML and PHP., (*9)

Rendering a simple template

This is the view/hello-world.phtml view script. No PHP is present, (*10)

<!-- view/hello-world.phtml-->
Hello, World!

To render the template, the path to the view script file must be passed as first parameter of the render method, (*11)

$template = realpath('view/hello-world.phtml');

$renderer = new \Lfj\ZfRenderer\Renderer();

/** @var \Zend\View\View $view */
$view = $renderer->render($template);

echo $view->getResponse()->getContent();

The render method returns an instance of \Zend\View\View. The content can be retrieved with $view->getResponse()->getContent()., (*12)

Passing data to the template

A template can make use of data contained in variables. This is an example, (*13)

<!-- view/hello-name.phtml -->
Hello, <?=$name?>!

To pass data to the view script, an associative array must be provided as the second argument of the render method, (*14)

$template = realpath('view/hello-name.phtml');

$renderer = new \Lfj\ZfRenderer\Renderer();
$view = $renderer->render($template, array('name' => 'World'));

echo $view->getResponse()->getContent();

Including partials

It could be useful to include other view scripts from within another view script. This can be done passing a list of template path resolvers to the withResolvers method. The method returns a new instance of $renderer., (*15)

This is the view/hello-partial.phtml view script which includes a partial, (*16)

Hello, <?=$this->partial('partial/name.phtml', array('name' => $name))?>!

and this is the partial/name.phtml file, (*17)

<?=$name?>

The code to render the whole thing is this, (*18)

$template = realpath('view/hello-partial.phtml');

$templatePathStack = new \Zend\View\Resolver\TemplatePathStack();
$templatePathStack->addPath(realpath('view'));

$renderer = new \Lfj\ZfRenderer\Renderer();
$renderer = $renderer->withResolvers(array($templatePathStack));

$renderedContent = $renderer->render(
    $template,
    array('name' => 'World')
);

echo $renderedContent->getResponse()->getContent();

The $templatePathStack object is a list of directories in which other view scripts can be found., (*19)

Adding helpers

Helpers can be added injecting an instance of Zend\View\HelperPluginManager using the setHelperPluginManager method., (*20)

The following view script uses the name helper., (*21)

Hello, <?=$this->name()?>!

An instance of the following PrintName class will be used the in the view script when $this->name() is called, (*22)

use Zend\View\Helper\AbstractHelper;
use Zend\View\Helper\HelperInterface;

class PrintName extends AbstractHelper implements HelperInterface
{
    public function __invoke()
    {
        echo "World";
    }
}

The code that puts all this together is the following, (*23)

$template = realpath('view/hello-helper.phtml');

$helperPluginManager = new \Zend\View\HelperPluginManager();
$helperPluginManager->setService('name', new PrintName());

$renderer = new \Lfj\ZfRenderer\Renderer();
$renderer = $renderer->withHelperPluginManager($helperPluginManager);
$renderedContent = $renderer->render(
    $template
);

echo $renderedContent->getResponse()->getContent();

The Versions

13/12 2016

dev-no-shared-events

dev-no-shared-events https://github.com/lorenzoferrarajr/lfj-zfrenderer

A utility to render PHP view scripts using the Zend Framework 2 PhpRenderer

  Sources   Download

MIT

The Requires

 

The Development Requires

02/10 2015

dev-master

9999999-dev https://github.com/lorenzoferrarajr/lfj-zfrenderer

A utility to render PHP view scripts using the Zend Framework 2 PhpRenderer

  Sources   Download

MIT

The Requires

 

The Development Requires

12/06 2015

dev-test-failing-because-of-event-manager

dev-test-failing-because-of-event-manager https://github.com/lorenzoferrarajr/lfj-zfrenderer

A utility to render PHP view scripts using the Zend Framework 2 PhpRenderer

  Sources   Download

MIT

The Requires

 

The Development Requires