Skip to content

Commit

Permalink
Updated module to be compatible with beta4 version of ZF2.
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmin-harangus committed May 24, 2012
1 parent 7a4b2c0 commit 85ec6fe
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 483 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2012 by the ZendExperts Team, see AUTHORS for more details.
Copyright (c) 2012 to ZendExperts Team, see AUTHORS for more details.

Some rights reserved.

Expand Down
73 changes: 21 additions & 52 deletions Module.php
@@ -1,5 +1,4 @@
<?php

/**
* This file is part of ZeTwig
*
Expand All @@ -10,60 +9,26 @@
*/
namespace ZeTwig;

use Zend\Module\Manager,
Zend\EventManager\StaticEventManager,
Zend\Module\Consumer\AutoloaderProvider,
Zend\Module\ModuleEvent,
Zend\EventManager\Event;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface,
Zend\Mvc\MvcEvent;

/**
* ZeTwig Module class
* @package ZeTwig
* @author Cosmin Harangus <cosmin@zendexperts.com>
*/
class Module implements AutoloaderProvider
class Module implements AutoloaderProviderInterface
{
/**
* @var \Zend\Mvc\AppContext
*/
protected static $application;

/**
* Module initialization
* @param \Zend\Module\Manager $moduleManager
* @var \Zend\ServiceManager\ServiceManager
*/
public function init(Manager $moduleManager)
{
$events = StaticEventManager::getInstance();
$events->attach('bootstrap', 'bootstrap', array($this, 'bootstrap'), 100);
}

public function bootstrap($event)
{
// Register a "render" event, at high priority (so it executes prior
// to the view attempting to render)
$app = $event->getParam('application');
static::$application = $app;
$app->events()->attach('render', array($this, 'registerTwigStrategy'), 100);
}
protected static $serviceManager;

public function registerTwigStrategy(Event $event)
public function onBootstrap(MvcEvent $event)
{
$app = $event->getTarget();
$locator = $app->getLocator();
$view = $locator->get('Zend\View\View');
$twigStrategy = $locator->get('ZeTwig\View\Strategy\TwigRendererStrategy');

$renderer = $twigStrategy->getRenderer();
$basePath = $app->getRequest()->getBasePath();
$renderer->plugin('basePath')->setBasePath($basePath);
$renderer->plugin('url')->setRouter($event->getRouter());
$renderer->plugin('headTitle')
->setSeparator(' - ')
->setAutoEscape(false);

// Attach strategy, which is a listener aggregate, at high priority
$view->events()->attach($twigStrategy, 100);
// Set the static service manager instance so we can use it everywhere in the module
$app = $event->getApplication();
static::$serviceManager = $app->getServiceManager();
}

/**
Expand All @@ -80,32 +45,36 @@ public function getAutoloaderConfig()
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
'prefixes' => array(
'Twig' => __DIR__ . '/vendor/Twig/lib/Twig'
)
),
);
}

/**
* Get Service Configuration
* @return array
*/
public function getServiceConfiguration(){
return include __DIR__ . '/config/service.config.php';
}

/**
* Get Module Configuration
* @return mixed
*/
public function getConfig()
{
$definitions = include __DIR__ . '/config/module.di.config.php';
$config = include __DIR__ . '/config/module.config.php';
$config = array_merge_recursive($definitions, $config);
return $config;
}

/**
* Return the ServiceManager instance
* @static
* @return \Zend\Mvc\AppContext
* @return \Zend\ServiceManager\ServiceManager
*/
public static function getApplication()
public static function getServiceManager()
{
return static::$application;
return static::$serviceManager;
}

}
68 changes: 54 additions & 14 deletions README.md
@@ -1,42 +1,82 @@
ZeTwig
====

ZeTwig is a Twig / Zend Framework 2 module compatible with ViewModels from beta3.
ZeTwig is a Twig / Zend Framework 2 module compatible the beta4 version of the framework.
It allows to render view templates using Twig instead of the default PHP templates.
It also supports aliases for your template names, rendering a particular action from
within the template files (follows the save naming conventions as Symfony) and
triggering events on an object with different parameters.

Instalation
-----------

ZeTwig can be installed using Composer by simply adding the following lines to your composer.json file:

```json
"require": {
"ZendExperts/ZeTwig": "1.0.*"
}
```

Then run `php composer.phar update`.

The module also defines a set of options that you can change from within the configuration files:

```php
'zendexperts_zetwig' => array(
//you can change the extension of the loaded templates here
'template_suffix' => 'twig',
'extensions' => array(
//add any extensions you want to register with twig
),
//set twig environment options
'environment_options' => array(
'cache' => BASE_PATH . '/data/cache/twig',
'auto_reload' => true,
'debug' => true
),
),
```

Documentation
-------------

Upgraded to the new beta3 version of ZF2 View Models.
2012.05.25: Upgraded to the beta4 version of ZF2 View Models.

Any command from the original Twig library should work and also added support for
Zend View helpers as functions and PHP functions as a fallback.

With this new update you should be able to use template names as aliases that can be
mapped to any twig file.

You can define an array for aliases within the configuration file for your modules and
use those aliases throughout your code, instead of a specific file name. This way you
can easily change the main layout of your pages from the configuration file and allow
other modules to change them as well (this allows your code to be extensible and allows
templates to have their own structure).

This latest version also contains two new constructs:
Apart from the functionality listed above the module adds two extension tags:

1. A tag for rendering a controller action, which follows the Symfony naming conventions
or the controller alias and can be used as :

1. A tag for rendering a controller action, which follows the Symfony naming conventions
or the controller alias:

```html
{% render "Core:Index:index" %}
```

The twig tag will call the action "index" from the "IndexController" located within the "Core"
module and based on the returned value it will either render a specific template or output the
returned value, following the same principles as with any other zf2 action.

Optionally you can also specify different parameters to send to the processed action which can
later be retrieved from the matched route:

```html
{% render "Core:Index:index" with {'param1':1} %}
```

1. A tag for triggering an event on the renderer that is similar to the above syntax:


```html
{% trigger "myRendererEvent" on myObject with {'param1':1} %}
```

Both the target object and parameters are optional. The result of each listener is
converted to string and rendered intead of the definition.

Also a new functionality allows the use of aliases within your template code or when
rendering a template.
converted to string and rendered intead of the definition.
26 changes: 14 additions & 12 deletions autoload/classmap.php
Expand Up @@ -4,16 +4,18 @@
*/
$prefix = dirname(__DIR__) . "";
return array(
'ZeTwig\Module' => $prefix . '/Module.php',
'ZeTwig\View\Environment' => $prefix . '/src/ZeTwig/View\Environment.php',
'ZeTwig\View\Extension' => $prefix . '/src/ZeTwig/View\Extension.php',
'ZeTwig\View\HelperFunction' => $prefix . '/src/ZeTwig/View\HelperFunction.php',
'ZeTwig\View\Renderer' => $prefix . '/src/ZeTwig/View\Renderer.php',
'ZeTwig\View\Resolver' => $prefix . '/src/ZeTwig/View\Resolver.php',
'ZeTwig\View\Exception\TemplateException' => $prefix . '/src/ZeTwig/View\Exception\TemplateException.php',
'ZeTwig\View\Extension\Render\RenderNode' => $prefix . '/src/ZeTwig/View\Extension\Render\RenderNode.php',
'ZeTwig\View\Extension\Render\TokenParser' => $prefix . '/src/ZeTwig/View\Extension\Render\TokenParser.php',
'ZeTwig\View\Extension\Trigger\RenderNode' => $prefix . '/src/ZeTwig/View\Extension\Trigger\RenderNode.php',
'ZeTwig\View\Extension\Trigger\TokenParser' => $prefix . '/src/ZeTwig/View\Extension\Trigger\TokenParser.php',
'ZeTwig\View\Strategy\TwigRendererStrategy' => $prefix . '/src/ZeTwig/View\Strategy\TwigRendererStrategy.php',
'ZeTwig\Module' => $prefix . '/Module.php',
'ZeTwig\View\Environment' => $prefix . '/src/ZeTwig/View\Environment.php',
'ZeTwig\View\Extension' => $prefix . '/src/ZeTwig/View\Extension.php',
'ZeTwig\View\HelperFunction' => $prefix . '/src/ZeTwig/View\HelperFunction.php',
'ZeTwig\View\Renderer' => $prefix . '/src/ZeTwig/View\Renderer.php',
'ZeTwig\View\Resolver' => $prefix . '/src/ZeTwig/View\Resolver.php',
'ZeTwig\View\Exception\TemplateException' => $prefix . '/src/ZeTwig/View\Exception\TemplateException.php',
'ZeTwig\View\Extension\Render\RenderNode' => $prefix . '/src/ZeTwig/View\Extension\Render\RenderNode.php',
'ZeTwig\View\Extension\Render\TokenParser' => $prefix . '/src/ZeTwig/View\Extension\Render\TokenParser.php',
'ZeTwig\View\Extension\Trigger\RenderNode' => $prefix . '/src/ZeTwig/View\Extension\Trigger\RenderNode.php',
'ZeTwig\View\Extension\Trigger\TokenParser' => $prefix . '/src/ZeTwig/View\Extension\Trigger\TokenParser.php',
'ZeTwig\View\Strategy\TwigRendererStrategy' => $prefix . '/src/ZeTwig/View\Strategy\TwigRendererStrategy.php',
'ZeTwig\View\Service\ViewTwigRendererFactory' => $prefix . '/src/ZeTwig/View\Service\ViewTwigRendererFactory.php',
'ZeTwig\View\Service\ViewTwigStrategyFactory' => $prefix . '/src/ZeTwig/View\Service\ViewTwigStrategyFactory.php',
);
92 changes: 21 additions & 71 deletions config/module.config.php
@@ -1,77 +1,27 @@
<?php
return array(
'di' => array(
'instance' => array(
'alias'=>array(
'view'=>'ZeTwig\View\Renderer'
),
// Inject the plugin broker for controller plugins into
// the action controller for use by all controllers that
// extend it.
// 'Zend\Mvc\Controller\ActionController' => array(
// 'parameters' => array(
// 'broker' => 'Zend\Mvc\Controller\PluginBroker',
// ),
// ),
// 'Zend\Mvc\Controller\PluginBroker' => array(
// 'parameters' => array(
// 'loader' => 'Zend\Mvc\Controller\PluginLoader',
// ),
// ),
// 'Zend\View\Resolver\TemplateMapResolver' => array(
// 'parameters' => array(
// 'map' => array(
// ),
// ),
// ),
'Zend\View\Resolver\TemplatePathStack' => array(
'parameters' => array(
'defaultSuffix'=>'twig',
),
),
'ZeTwig\View\Resolver'=>array(
'injections' => array(
'Zend\View\Resolver\TemplateMapResolver',
'Zend\View\Resolver\TemplatePathStack',
),
),
'ZeTwig\View\Renderer' => array(
'parameters' => array(
'broker' => 'Zend\View\HelperBroker',
'environment'=>'ZeTwig\View\Environment',
),
),
'Zend\Mvc\View\DefaultRenderingStrategy' => array(
'parameters' => array(
'layoutTemplate' => 'layouts/layout',
),
),
'Zend\Mvc\View\ExceptionStrategy' => array(
'parameters' => array(
'displayExceptions' => true,
'exceptionTemplate' => 'error/index',
),
),
'Zend\Mvc\View\RouteNotFoundStrategy' => array(
'parameters' => array(
'displayNotFoundReason' => true,
'displayExceptions' => true,
'notFoundTemplate' => 'error/404',
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'layout' => 'layout/layout',
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'strategies' => array(
'ze-twig' => 'ViewTwigRendererStrategy',
)
),

'zendexperts_zetwig' => array(
'template_suffix' => 'twig',
'extensions' => array(
'ZeTwig' => 'ZeTwig\View\Extension'
),
'ZeTwig\View\Environment'=>array(
'injections' => array(
'ZeTwig'=>'ZeTwig\View\Extension'
),
'parameters' => array(
'loader' => 'ZeTwig\View\Resolver',
'options' => array(
'cache' => BASE_PATH . '/data/cache/twig',
'auto_reload' => true,
'debug' => true
),
),
'environment_options' => array(
'cache' => BASE_PATH . '/data/cache/twig',
'auto_reload' => true,
'debug' => true
),
),
),

);

0 comments on commit 85ec6fe

Please sign in to comment.