Symfony1中Symfony2等价的组件是什么?

时间:2021-03-02 20:21:07

In my Symfony2 application, I want to have a widget displayed on various pages. This can't just be defined by its template, it need to call the DB and go through the controller.

在我的Symfony2应用程序中,我希望在各个页面上显示一个小部件。这不仅可以通过其模板定义,还需要调用DB并通过控制器。

In Symfony1, I would create a component and include it. How can I do the same in Symfony2?

在Symfony1中,我将创建一个组件并包含它。我怎样才能在Symfony2中做同样的事情?

2 个解决方案

#1


27  

I did some more research, and the simplest way I found was just this simple line in the template:

我做了更多的研究,我发现的最简单的方法就是在模板中使用这个简单的行:

{% render 'MyBundle:MyController:myAction' %}

This outputs the result of the action, with the template specified by the action.

这将使用操作指定的模板输出操作的结果。

#2


8  

You can create Twig Extension with function widget and register it in the container. Also inject Kernel into this extension.

您可以使用功能小部件创建Twig扩展并将其注册到容器中。同时将Kernel注入此扩展。

class WidgetFactoryExtension extends \Twig_Extension
{
    protected $kernel;

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

    public function getFunctions()
    {
        return array(
            'widget' => new \Twig_Function_Method($this, 'createWidget', array('is_safe' => array('html'))),
        );
    }

    public function createWidget($name, array $options = array())
    {
        list($bundle, $widget) = explode(':', $name);

        $widgetClass = $this->kernel->getBundle($bundle)->getNamespace() . '\\Widget\\' . $widget;
        $widgetObj = new $widgetClass();

        $widgetObj->setContainer($this->kernel->getContainer());

        if ($options) {
            $widgetObj->setOptions($options);
        }

        return $widgetObj;
    }
}

And after this write in templates:

在写完模板之后:

{{ widget('QuestionsBundle:LastAnswers', {'answersCount' : 10}) }}
{# class QuestionsBundle/Widget/LastAnswers #}

#1


27  

I did some more research, and the simplest way I found was just this simple line in the template:

我做了更多的研究,我发现的最简单的方法就是在模板中使用这个简单的行:

{% render 'MyBundle:MyController:myAction' %}

This outputs the result of the action, with the template specified by the action.

这将使用操作指定的模板输出操作的结果。

#2


8  

You can create Twig Extension with function widget and register it in the container. Also inject Kernel into this extension.

您可以使用功能小部件创建Twig扩展并将其注册到容器中。同时将Kernel注入此扩展。

class WidgetFactoryExtension extends \Twig_Extension
{
    protected $kernel;

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

    public function getFunctions()
    {
        return array(
            'widget' => new \Twig_Function_Method($this, 'createWidget', array('is_safe' => array('html'))),
        );
    }

    public function createWidget($name, array $options = array())
    {
        list($bundle, $widget) = explode(':', $name);

        $widgetClass = $this->kernel->getBundle($bundle)->getNamespace() . '\\Widget\\' . $widget;
        $widgetObj = new $widgetClass();

        $widgetObj->setContainer($this->kernel->getContainer());

        if ($options) {
            $widgetObj->setOptions($options);
        }

        return $widgetObj;
    }
}

And after this write in templates:

在写完模板之后:

{{ widget('QuestionsBundle:LastAnswers', {'answersCount' : 10}) }}
{# class QuestionsBundle/Widget/LastAnswers #}