5

Let's say I have comments in more than one spot in website. How can I create something like {{ render_widget('comments', {"object": object} ) }} ? That would render the form and list with all comments for that object ?

3
  • Maybe try to use service for this? Commented Jan 20, 2014 at 20:28
  • And then pass this service to a twig template. Commented Jan 20, 2014 at 21:05
  • @Victor please, give me an example code. :) Commented Jan 20, 2014 at 21:29

2 Answers 2

5

Create a service:

// src/Acme/HelloBundle/Service/Widget.php
namespace Acme\HelloBundle\Service;

use Symfony\Component\DependencyInjection\ContainerInterface;

class Widget
{
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function getComments()
    {
        $request = $this->container->get('request'); // use service_container to access request, doctrine, twig, etc...
    }
}

Declare a service:

# src/Acme/HelloBundle/Resources/config/services.yml
parameters:
    # ...
    my_widget.class: Acme\HelloBundle\Service\Widget

services:
    my_widget:
        class:     "%my_widget.class%"
        arguments: ["@service_container"]
        # scope: container can be omitted as it is the default

Use a service in controller:

namespace Acme\HelloBundle\Controller;

class BlogController {

    public function getPostAction($id) {
        // get post from db
        $widget = $this->get('my_widget'); // get your widget in controller
        $comments = $widget->getComments(); // do something with comments

        return $this->render('AcmeHelloBundle:Blog:index.html.twig',
            array('widget' => $widget) // pass widget to twig
        );
    }
}

or in twig, if you pass your service in template like above in render() function:

#AcmeHelloBundle:Blog:index.html.twig

{{ widget.getComments()|raw }}

And usefull to read the docs about How to work with Scopes

Sign up to request clarification or add additional context in comments.

5 Comments

And how can get instance of service in Twig ?
I'm to, but I have not yet found a suggestion how do not use |raw filter. If you found - tell me ;)
For get instance of service in Twig you must to pass it service to twig template, like in my example return $this->render('AcmeHelloBundle:Blog:index.html.twig', array('widget' => $widget) // pass widget to twig );
Yes, but I want to get instance directly in Twig. Let's say I have widget which renders comment icon for news, I have to foreach news in php and then in twig again, I want only once to foreach them in twig. :)
For only once to foreach them in twig get them in __construct() method of your Widget service. The __construct() method call only once when you first call a service, so you further work with a singleton
2

I have done it another way. I registered Twig Extension with function {{ comments(object) }}

The function is registered that way

'comments' => new \Twig_Function_Method($this, 'comments', array(
        'needs_environment' => true,
        'is_safe' => array('html')
            ))

That way I don't need to specify |raw filter.

2 Comments

What is object in {{ comments(object) }}?
Variable ? :D The commented object

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.