且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 Symfony2 中的树枝模板中获取数据?

更新时间:2022-04-11 04:46:07

添加到我上面的评论中:

To add to my comment above:

您将有一个控制器,用于获取数据:

You'd have a controller with an action to fetch your data:

Acme\SomeBundle\Controller\DataController.php

/**
 * @Template
 */
public function fetchDataAction($someParam, $quantity) {
    $data = doSomethingWithDatabase();

    return array('data' => $data);
}

Acme\SomeBundle\Resources\view\Data\fetchData.html.twig

{% for item in data %}
    {{ item.name }}
    {{ item.title }}
{% endfor %}

然后在您的 template1template2 中,如果合适,您可以对您的值进行硬编码,或者使用分别传递给这些模板的值.

then in your template1 and template2 you can hardcode your values if that suits, or use values that are passed to those templates respectively.

{% render 'AcmeSomeBundle:Data:fetchData' with {'someParam': 'something', 'quantity': 20} %}