How can I render a controller action within a twig extension?

Twoez

I do have a twig extension which has a method that uses another method from a different controller to render a json output via dependency jsonResponse.

How can I render a controller within a twig extension?

The following code below doesn't seem to work, because render() needs a view file instead of a controller. And I am now referencing to a controller.

class AreaExtension extends \Twig_Extension {

    public function add()
    {

        $outputJson = $this->container->get('templating')->render(new ControllerReference('CMSCoreBundle:Area:index'));
    }
}
Zsolt Gyöngyösi
$ref = new ControllerReference('CMSCoreBundle:Area:index');
$this->handler->render( $ref, 'inline', $options );

Where $this->handler is the fragment.handler service.

In your case:

$outputJson = $this->container->get('fragment.handler')->render(new ControllerReference('CMSCoreBundle:Area:index'));

You can find a full example in this symfony twig extension, see: https://github.com/symfony/symfony/blob/4.1/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php#L28 and https://github.com/symfony/symfony/blob/4.1/src/Symfony/Bridge/Twig/Extension/HttpKernelRuntime.php#L41

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I render a controller action within a twig extension?

From Dev

How can I access to parameters.yml content within a custom twig extension

From Dev

How do I render a controller returned image in a Twig template

From Dev

How do I render a controller returned image in a Twig template

From Dev

Symfony2, how to render controller's action in a twig template if controller has a constructor

From Dev

How to render content within a custom twig function?

From Dev

How work render action in controller?

From Dev

How can I return a Json in controller action

From Java

How can I use break or continue within for loop in Twig template?

From Dev

How to insert a Controller in Twig with “render” in Silex

From Dev

How do I pass a parameter to a controller action within a Laravel Package?

From Dev

How can I use my php in Twig (custom controller?)

From Dev

How do I call another controller action from within a controller action?

From Dev

Can I render another controller's action from my current controller in Rails 4?

From Dev

How can I render a theme partial from within a component

From Dev

How can I render a partial view from within a layout file?

From Dev

How can I add a context menu to a browser action with a Chrome extension?

From Dev

How can I add a context menu to a browser action with a Chrome extension?

From Dev

How to render methodAction of another Controller inside an action

From Dev

How to render methodAction of another Controller inside an action

From Dev

Symfony Twig extension render view

From Dev

How can I call methods within controller (RoR)?

From Dev

How to call Twig extension filter or function from PHP (controller / service / other twig extension etc )?

From Dev

How can I make fields of a domain mandatory based on controller action?

From Dev

How can I check if there was a redirection on another controller action?

From Dev

How can I get the action name in a Symfony2 controller?

From Dev

how i can access an action method without defining the Controller name

From Dev

How can I run controller action with whenever gem in Rails?

From Dev

How can I post JSON to an ASP.NET controller action?

Related Related

  1. 1

    How can I render a controller action within a twig extension?

  2. 2

    How can I access to parameters.yml content within a custom twig extension

  3. 3

    How do I render a controller returned image in a Twig template

  4. 4

    How do I render a controller returned image in a Twig template

  5. 5

    Symfony2, how to render controller's action in a twig template if controller has a constructor

  6. 6

    How to render content within a custom twig function?

  7. 7

    How work render action in controller?

  8. 8

    How can I return a Json in controller action

  9. 9

    How can I use break or continue within for loop in Twig template?

  10. 10

    How to insert a Controller in Twig with “render” in Silex

  11. 11

    How do I pass a parameter to a controller action within a Laravel Package?

  12. 12

    How can I use my php in Twig (custom controller?)

  13. 13

    How do I call another controller action from within a controller action?

  14. 14

    Can I render another controller's action from my current controller in Rails 4?

  15. 15

    How can I render a theme partial from within a component

  16. 16

    How can I render a partial view from within a layout file?

  17. 17

    How can I add a context menu to a browser action with a Chrome extension?

  18. 18

    How can I add a context menu to a browser action with a Chrome extension?

  19. 19

    How to render methodAction of another Controller inside an action

  20. 20

    How to render methodAction of another Controller inside an action

  21. 21

    Symfony Twig extension render view

  22. 22

    How can I call methods within controller (RoR)?

  23. 23

    How to call Twig extension filter or function from PHP (controller / service / other twig extension etc )?

  24. 24

    How can I make fields of a domain mandatory based on controller action?

  25. 25

    How can I check if there was a redirection on another controller action?

  26. 26

    How can I get the action name in a Symfony2 controller?

  27. 27

    how i can access an action method without defining the Controller name

  28. 28

    How can I run controller action with whenever gem in Rails?

  29. 29

    How can I post JSON to an ASP.NET controller action?

HotTag

Archive