Giter Club home page Giter Club logo

zf3-twig's Introduction

ZendTwig module for Zend Framework 3

Latest Stable Version Total Downloads codecov.io Build Status Software License

ZendTwig is a module that integrates the Twig template engine with Zend Framework 3 / Laminas Project.

Notes

  • From release v1.0.0 this project depends on Laminas Project components.
  • Development of package udner PHP5.6 has low priority and will be discontinued in closest feature.

Install

  1. Add ZendTwig lib with composer: composer require oxcom/zend-twig
  2. Add ZendTwig to Your config/application.config.php file as module:
    // Retrieve list of modules used in this application.
    'modules'                 => [
        'Zend\Router',
        'Zend\Validator',
        'Zend\I18n',
        'Zend\Mvc\I18n',
        'Application',
        // ...
        'ZendTwig',
    ],

Setting up

Here You can find some examples, configurations and e.t.c. that, I hope, will help You do build Your application. Short list of available docs:

  1. ZendTwig module
  2. Custom Twig Extensions
  3. TwigModel

Bugs and Issues

Please, if You found a bug or something, that is not working properly, contact me and tell what's wrong. It's nice to have an example how to reproduce a bug, or any idea how to fix it in Your request. I'll take care about it ASAP.

zf3-twig's People

Contributors

oxcom avatar reinfi avatar vojtabiberle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

zf3-twig's Issues

I can't activate the debug mode

Hi,

During my development, I often use the tag dump () to check the contents of my variables.
I tried to activate this mode via a configuration file, but I had no result
Here is my configuration fde file in config / autoload:

`Use Application \ Twig \ Extension \ TwigFilterExtension;
Use Application \ Twig \ Extension \ TwigFunctionExtension;
Use Application \ Twig \ Extension \ TwigTestExtension;
Use Application \ Twig \ Extension \ TwigTagExtension;
return [
'Zend_twig' => [
'Force_standalone' => true,
'Invoke_zend_helpers' => true,
'Environment_options' => [
'Cache' => false,
'Debug' => true
],
'Loader_chain' => [
\ZendTwig\Loader\MapLoader::class,
\ZendTwig\Loader\StackLoader::class,
],
'Extensions' => [
\ZendTwig\Extension\Extension::class,
'Debug' => 'Twig_Extension_Debug',
'LoadString' => 'Twig_Extension_StringLoader',
'PersonalFilter' => TwigFilterExtension::class,
'PersonalFunction' => TwigFunctionExtension::class,
'PersonalTest' => TwigTestExtension::class,
'PersonalNode' => TwigTagExtension::class,
],
'Helpers' => [
'Configs' => [
\Zend\Navigation\View\HelperConfig::class,
],
],
],
];

Did I configure my application correctly?
Is there another way to code this activation?

Thank you in advance for your help.

Gilbert ARMENGAUD
Celtic34fr
[email protected]
(Béziers - Herault - Occitania - France)

Unable to resolve service "Twig_Environment" to a factory

After installing from composer, and adding ZendTwig in modules.config.php, i get this error:

Fatal error: Uncaught Zend\ServiceManager\Exception\ServiceNotFoundException: Unable to resolve service "Twig_Environment" to a factory; are you certain you provided it during configuration? in /home/makor/webprojects/recording-exchange.recex/vendor/zendframework/zend-servicemanager/src/ServiceManager.php:681 Stack trace: #0 /home/makor/webprojects/recording-exchange.recex/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(757): Zend\ServiceManager\ServiceManager->getFactory('Twig_Environmen...') #1 /home/makor/webprojects/recording-exchange.recex/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(200): Zend\ServiceManager\ServiceManager->doCreate('Twig_Environmen...') #2 /home/makor/webprojects/recording-exchange.recex/vendor/oxcom/zend-twig/src/Module.php(30): Zend\ServiceManager\ServiceManager->get('Twig_Environmen...') #3 /home/makor/webprojects/recording-exchange.recex/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): ZendTwig\Module->onBootstrap(Object(Zend\Mvc\MvcEvent)) #4 in /home/makor/webprojects/recording-exchange.recex/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 681

How to fix this?:(

Thanks!

Re-implement getHelperPluginManager in TwigRenderer ?

Hello,

I'm working on a ZF3 module of sass compiler, and I need to call the method getHelperPluginManager that implemented in PhpRenderer, but this method has been rename on getZendHelpers in your TwigRenderer.

Actually, I need to test the type of renderer for call the good method, but it will be simplest not to do this.

    public function prepareStyle(ViewEvent $e) {
        /** @var \Zend\View\Renderer\PhpRenderer|\ZendTwig\Renderer\TwigRenderer $renderer */
        $renderer = $e->getRenderer();
        if ($renderer instanceof \ZendTwig\Renderer\TwigRenderer) {
            $pluginManager = $renderer->getZendHelpers();
        } else {
            $pluginManager = $renderer->getHelperPluginManager();
        }
        $containerHeadLink = $pluginManager->get('headLink')->getContainer();
        // ...
    }

So is it possible to re-implement this method in TwigRenderer ?

JsonModel not work

Yo.., allright?

In my Controller, will return JsonModel instance, but when call method, return:

ZendTwig\Renderer\TwigRenderer::render: Unable to render template "user/api/user-authentication/authenticate"; resolver could not resolve to a file

Gist: https://gist.github.com/guilherme90/33e97890ce0e8c4827a4146dc704f206

And yes, i set ViewJsonStrategy in view_manager -> strategies

How can I solve this? Thanks :)

Extends does not work

Hi,
The problem occured:
if we use extends and {{content|raw}} in parent it renders the whole page instead of block content,
if we don't include content|raw it doesn't render block at all
Are we doing something wrong?

File: layout/layout.twig

{% block header %}{% endblock %}
<section class="container">
    <!-- Page content placeholder -->
    {% block content %}{% endblock %}
</section>
{% block footer %}{% endblock %}

File: application/index/index.twig

{% extends 'layout/layout.twig' %}
{% block header %}
test header
{% endblock %}

{% block content %}
test content
{% endblock %}

problem render action

I installed laminas mvc and the zend-twig package, and it does not render the action, only the template.
Can you check?

broken after install

by default, module contains \Zend\Navigation\View\HelperConfig::class

need add zendframework/zend-navigation to composer.json or remove from default configuration, else:

Uncaught Zend\View\Exception\RuntimeException: Unable to resolve provided configuration to valid instance of Zend\ServiceManager\ConfigInterface in //vendor/oxcom/zend-twig/src/Service/TwigHelperPluginManagerFactory.php:56

Why not embed instanceOf as an extension built into ZendTwig

Hi,
I use Twig in my personal project GraphicObjectTemplating (ZF2/ZF3).
Giving access to TWIG under ZF2 / ZF3 is interesting, but having an adaptation of the basic templates would be more useful, as your extension instanceOf.
If this can help you, I would have both extensions and basic templates to offer you

  • layout/layout, error/index, error/404, application/index/index (templates),
  • intanceOf, typeOf (test extensions),
  • getclass, gettype, arrayexception (for error/index template), checkBoolean, substr, strpos, instring (function extensions),
  • contains (subchain is in string), update (update of table by key / value), unset (deletion occurance in array by key) (filter extension)

If you are interested, I can send you the ZF3 sources by email at your request, not all in my project on github (celti34fr / graphic-object-templating)

I found in the TWIG repository a tag refused by Fabien POTENCIER: switch/case that I adapted, I can also provide you.

Gilbert ARMENGAUD
celtic34fr
[email protected]
(Béziers - Hérault - Occitanie - France)

Can't use JsonModel and TwigModel

Hi

I'm having the same problem as #9, when returning a JsonModel instance, I got this fatal error instead of the json response.

 Fatal error: Uncaught Zend\View\Exception\RuntimeException: ZendTwig\Renderer\TwigRenderer::render: Unable to render template "xxx"; resolver could not resolve to a file in /xxx/vendor/oxcom/zend-twig/src/Renderer/TwigRenderer.php on line 185

If I set force_twig_strategy to false, the JsonModel returns the json correctly, but then TwigModel does not render the twig templates

There's something I can do to solve this problem? Thank you all

Laminas update

Hey!

Would you like to update this library to Laminas or add a new package because of zf3 in its package name?

Best regards
Martin

TwigRenderer is being forced despite settings

Hi.

I have the 'force_twig_strategy' option disabled and my view model (that gets passed into the rendering strategies through a ViewEvent object) is an instance of Laminas\View\Model\ViewModel - but TwigRenderer still kicks in for some of the templates.

This happens due to the following piece of code:

if ($this->renderer instanceof TwigRenderer) {
try {
$tpl = $this->renderer->getResolver()->resolve($model->getTemplate());
if ((class_exists('\Twig\TemplateWrapper') && ($tpl instanceof \Twig\TemplateWrapper))
|| (class_exists('\Twig\Template') && ($tpl instanceof \Twig\Template))
) {
return $this->renderer;
}
} catch (\Throwable $e) {
}
}

If a Twig loader is able to load a .twig template by the same name (as my intended template) - it'll force this view model to be rendered with a TwigRenderer.

Custom view path

I am using custom script/layout paths and its not being recognized with twig.

Broken after install of the module

I have this error
Uncaught Laminas\Di\Exception\MissingPropertyException: Could not resolve value for parameter "serviceManager" of type Interop\Container\ContainerInterface in class ZendTwig\Extension\Extension (requested as ZendTwig\Extension\Extension) in /home/esokia/Labs/laminas/vendor/laminas/laminas-di/src/Resolver/DependencyResolver.php:329
Uncaught Laminas\Di\Exception\MissingPropertyException: Could not resolve value for parameter "serviceManager" of type Interop\Container\ContainerInterface in class ZendTwig\Extension\Extension (requested as ZendTwig\Extension\Extension) in /home/esokia/Labs/laminas/vendor/laminas/laminas-di/src/Resolver/DependencyResolver.php:329

Access view helpers

Hello,

I am trying to access view helpers. They are defined like this:

    'view_helpers' => [
        'factories' => [
            View\Helper\Menu::class => View\Helper\Factory\MenuFactory::class,
            View\Helper\Breadcrumbs::class => InvokableFactory::class,
        ],
        'aliases' => [
            'mainMenu' => View\Helper\Menu::class,
            'pageBreadcrumbs' => View\Helper\Breadcrumbs::class,
        ],
    ],

Helper call:

{{ mainMenu().setActiveItemId('permission') }}

This is the skelleton app where i took that from, I converted that phtml file to twig:
https://github.com/olegkrivtsov/using-zf3-book-samples/blob/master/roledemo/module/User/view/user/permission/index.phtml

Error:

Twig_Error_Runtime: Impossible to invoke a method ("setActiveItemId") on a null variable. in /home/sidia/shared/Arena/module/User/view/user/permission/index.twig:3 Stack trace: #0 /home/sidia/shared/Arena/data/cache/twig/ef/efd40460f70103b4618fd327bb091ad43518b4be9e54f209dad7909d4f9ffee2.php(24): twig_get_attribute(Object(Twig_Environment), Object(Twig_Source), NULL, 'setActiveItemId', Array, 'method') #1 /home/sidia/shared/Arena/vendor/twig/twig/lib/Twig/Template.php(389): __TwigTemplate_2f9cce762ce8ae9a5d5d1dbef906fd226a1b81292f71d82b398389a696db6e06->doDisplay(Array, Array) #2 /home/sidia/shared/Arena/vendor/twig/twig/lib/Twig/Template.php(366): Twig_Template->displayWithErrorHandling(Array, Array) #3 /home/sidia/shared/Arena/vendor/twig/twig/lib/Twig/Template.php(374): Twig_Template->display(Array) #4 /home/sidia/shared/Arena/vendor/oxcom/zend-twig/src/Renderer/TwigRenderer.php(221): Twig_Template->render(Array) #5 /home/sidia/shared/Arena/vendor/oxcom/zend-twig/src/Renderer/TwigRenderer.php(202): ZendTwig\Renderer\TwigRenderer->render('user/permission...', Array) #6 /home/sidia/shared/Arena/vendor/zendframework/zend-view/src/View.php(207): ZendTwig\Renderer\TwigRenderer->render('layout/layout') #7 /home/sidia/shared/Arena/vendor/zendframework/zend-mvc/src/View/Http/DefaultRenderingStrategy.php(105): Zend\View\View->render(Object(Zend\View\Model\ViewModel)) #8 /home/sidia/shared/Arena/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent)) #9 /home/sidia/shared/Arena/vendor/zendframework/zend-eventmanager/src/EventManager.php(171): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent)) #10 /home/sidia/shared/Arena/vendor/zendframework/zend-mvc/src/Application.php(367): Zend\EventManager\EventManager->triggerEvent(Object(Zend\Mvc\MvcEvent)) #11 /home/sidia/shared/Arena/vendor/zendframework/zend-mvc/src/Application.php(348): Zend\Mvc\Application->completeRequest(Object(Zend\Mvc\MvcEvent)) #12 /home/sidia/shared/Arena/public/index.php(42): Zend\Mvc\Application->run() #13 {main}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.