0

I have two twig extensions and a twig. I need functions from both the twig extensions in one twig. What is the way to differentiate that from which extension the function is called.

Right now, I'm using this method driverId|getDriverNameFromId and it is giving me error!

Below is the code for driver extension:

<?php

namespace abc\CoreBundle\Extension;

use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use abc\CoreBundle\Entity\CostAllocation;
use abc\CoreBundle\Entity\Driver;

class DriverExtension extends \Twig_Extension {

    protected $mr;
    protected $container;

    public function __construct(ManagerRegistry $mr, Container $container) 
    {
        $this->mr = $mr;
        $this->container = $container;
    }

    public function getFilters() 
    {
        return array(
            new \Twig_SimpleFilter('getDriverNameFromId', array($this, 'getDriverNameFromId')),
        );
    }

    public function getName() 
    {
        return 'driver_extension';
    }

    /**
     * get updated columns
     *
     * @param Integer $driverImportId
     *
     * return array
     */
    public function getDriverNameFromId($driverImportId)
    {
        return $this->container->get('core_process_import')->getName($driverImportId, 'DriverImport');
    }


}

And similarly I have a core extension.

In my twig, when I call

    {% if driverImport.id|getDriverNameFromId|length > 0  %}
{% endif %}

It gives me error

Unknown "getDriverNameFromId" filter in CoreBundle:DriverImport:list.html.twig at line 88?

Below is the code for services.yml

_core.services.invoice.extension.class: abc\CoreBundle\Extension\InvoiceExtension
    _core.services.driver.extension.class: abc\CoreBundle\Extension\DriverExtension


invoice.twig.extension: 
        class: %_core.services.invoice.extension.class% 
        arguments: [@doctrine, @service_container] 
        tags: 
            - { name: twig.extension }
    driver.twig.extension: 
        class: %_core.services.driver.extension.class% 
        arguments: [@doctrine, @service_container] 
        tags: 
            - { name: twig.extension }
13
  • Provide the code to expose the issue. Commented Jul 13, 2016 at 8:30
  • please refer to the question again. I've updated the description with code Commented Jul 13, 2016 at 8:35
  • why not put all your filters/functions in the same class? Commented Jul 13, 2016 at 8:43
  • You can have as many extensions as you want/need. So there being two, is propably not the problem. It more seems like DriverExtension issn't loaded at all, and thus the filter is not available. Commented Jul 13, 2016 at 8:45
  • 1
    Just follow the manual, and check if/what you missed. Commented Jul 13, 2016 at 8:46

2 Answers 2

1

I've tested your code in my project and it works fine, so it's something else.

I think that your service configuration is not imported.

If you added it in bundle's services.yml file (not the main one in app/config/services.yml), then make sure that you've imported this file in config.yml file.

Assuming your service configuration is in

src/CoreBundle/Resources/config/services.yml

make sure you've imported it at the beggining of app/config/config.yml file:

imports:
    - { resource: parameters.yml }  // |
    - { resource: security.yml }    // |=> default ones
    - { resource: services.yml }    // |
    - { resource: "@CoreBundle/Resources/config/services.yml" } //new one
Sign up to request clarification or add additional context in comments.

1 Comment

I made a typo in services.yml. Thanks!
1

First get the namespace right (as per there suggestion for best pratice); it should be namespace Smartshore\CoreBundle\Twig for any Twig extension classes.

Make sure you have declared the classes correctly in your services.yml.
E.g.

driver_extension.twig.extension:
        class: Smartshore\CoreBundle\Twig\DriverExtension
        arguments: ["@my.injected.services"]
        tags:
           - { name: twig.extension }
myotherclass.twig.extension:
        class: Smartshore\CoreBundle\Twig\MyOtherTwigExtension
        arguments: ["@security.context", '@kernel', '@router']
        tags:
           - { name: twig.extension }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.