Call services in templates

2019-08-06 12:46发布

Following my last question I can't manage to call the service from the template. I did exactly like in the answer, but I get 'Variable "newmessages" does not exist' in the template.

I also tried returning the service in the parent template, but the parent is never called since there is no request to it, the requests are done only to child templates.

public function indexAction(){
    $locationService = $this->container->get('newmessages');


    return $this->render(
        'MedAppCrudBundle:UserBackend\Message:index.html.twig',
        array('newmessages'=>$locationService->methodToCalculate())

    );
}

How to call service from parent template? The answer from last question doesn't work.

2条回答
来,给爷笑一个
2楼-- · 2019-08-06 12:53

You can expose your service as a twig function and use it in the twig statement. See the full doc here. As example, define an extension as:

services.yml

#########  TWIG EXTENSION
    acme.message_helper_twig_extension:
        class: Acme\MessageBundle\Twig\MessageHelperExtension
        arguments: [@newmessages]
        tags:
            - { name: twig.extension }

And implement it as follow:

namespace Acme\MessageBundle\Twig\MessageHelperExtension;



class MessageHelperExtension extends \Twig_Extension {


    protected $newMessages;


    function __construct($newMessages)
    {
        $this->newMessages = $newMessages;
    }



    public function getFunctions()
    {
        return array(
            //////////////////////////////////
            'twig_method_reference_name'=> new \Twig_Function_Method($this, 'getMessages'),
        );
    }

    public function getMessages()
    {
        return $this->newMessages->getMessages()
    }
    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'message_helper';
    }

} 

Then you can use it in the twig as:

{{ twig_method_reference_name() }}

Hope this help

查看更多
Ridiculous、
3楼-- · 2019-08-06 13:14

The answer to your question is quite simple. You expose the service as a twig global variable. Based on your example, you should add something similar to your config.yml

twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    globals:
        newmessages: "@newmessages"

To use it in your twig, you would just write {{ newmessages.methodToCalculate() }}.

You can read more about this in the How to Inject Variables into all Templates documentation page.

Here is a more detailed example:

// Add this to config.yml
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    globals:
        newmessages: "@newmessages"

// Add this to services.yml (or if you're using older version of Symfony, add it to config.yml as well)
services:
    newmessages:
        class: App\YourBundle\Service\NewMessages

// The NewMessages service class
<?php

namespace App\YourBundle\Service;

class NewMessages {

    public function methodToCalculate() {
        return "location calculated.";
    }

}

?>

 // in your twig template do this
 {{ newmessages.methodToCalculate() }}  // outputs "location calculated."
查看更多
登录 后发表回答