Twig in Zend Framework 2

April 8th, 2012 by Cosmin Harangus Leave a reply »

The Zend Framework 2 library came a long way since it’s beta 1 version. In beta 3 you now have a full MVC stack, View Models, Database layer, a Mail component, a Cache component, Dependency Injection and an Event Manager.

In an attempt to extend the existing functionality of zend framework 2 and provide a powerful view layer for my applications I created a module which integrates the full functionality of the Twig library along with a new extension specific for zend.

The module allows you to use *.twig templates instead of the usual php *.phtml templates that are shipped by default in zend framework. This allows you to extend templates files, use concise syntax and extend the syntax with new tags or functionality.

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.

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 functionality comes directly with the new View Models from the framework.

The latest version also contains two new constructs:

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

{% render “Core:Index:index” with {‘param1′:1} %}

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

{% 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.

For more Zend Framework 2 module please visit: https://github.com/ZendExperts

Update: This module was merged into ZfcTwig and is currently available at: https://github.com/ZF-Commons/ZfcTwig . This will ensure better maintenance for the project and continuous improvements.

Share

5 comments

  1. Cosmin says:

    Hi, the ZeTwig module was merged with ZfcTwig for better support. You can find it at https://github.com/ZF-Commons/ZfcTwig

  2. Hi Cosmin , i know except from this subject but i need it.

    I have a controller and it call a plugin about langPlugin

    public function indexAction()
    {

    $lang = $this->LangPlugin();
    $lang->setLang($this);

    }

    I want to autoload my module and call my controller when it loaded. How can i made it ? When i call on onBootStrap it return me
    ” Uncaught exception ‘Zend\ServiceManager\Exception\ServiceNotFoundException’ with message ‘Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for LangPlugin’ in C:\wamp\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 420″

    Can u help me ? How can i made it ?

  3. Cosmin says:

    Hi,

    Not exactly sure what you want to do in bootstrap. Can you post your code here?

    Regards,
    Cosmin

  4. Thats my module.php (which i changed)

    use PrtLang\Controller\IndexController as MyController;

    $langa = new MyController();
    $langa->indexAction();

    //I mean , i want to call my controller from my module.php so i want to autoload my controllers code . How can i make it , and i need Param value ? Can i made it?

Leave a Reply