如何使用@Template() 引用多个树枝 [英] How to reference multiple twigs with @Template()

查看:22
本文介绍了如何使用@Template() 引用多个树枝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划更新在 Symfony 3.0 上运行的应用程序.
以前用@Template(),但是更新的时候必须在().
中指定twig文件的根目录但是,你需要改变_format.
中index.csv.twig和index.html.twig的读取有什么好的办法吗?

I plan to update the app that was running on Symfony 3.0.
I used to use @Template(), but when updating, I have to specify the root of the twig file in().
However, you need to change the reading of index.csv.twig and index.html.twig in _format.
Is there any good way?

之前(Symfony 3.0)

Before (Symfony 3.0)

    /**
     * @Route("/{_format}", defaults={"_format"="html"}, requirements={"_format"="html|csv"})
     * @Method("GET")
     *  
     * @Template()
     */
    public function indexAction(Request $request)
    {
        if ($request->getRequestFormat() == 'html') {
        } elseif ($request->getRequestFormat() == 'csv') {
            $request->attributes->set('filename', 'post_article.csv');
        }

之后(Symfony 3.4 <)

After (Symfony 3.4 <)

    /**
     * @Route("/{_format}", defaults={"_format"="html"}, requirements={"_format"="html|csv"})
     * @Method("GET")
     *  
     * @Template("AppBudle:Hq/Post:index.html.twig")
     */
    public function indexAction(Request $request)
    {
        if ($request->getRequestFormat() == 'html') {
        } elseif ($request->getRequestFormat() == 'csv') {
            $request->attributes->set('filename', 'post_article.csv');
        }

<button type="submit" class="btn">
       <i class="icon-download"></i>
       <a class="a_btn" href="{{ path('app_hq_post_index', {'_format': 'csv'}) }}"> output</a>
</button>

推荐答案

可以直接用$this->render代替@template

You can use directly $this->render instead of @template

/**
 * @Route("/{_format}", defaults={"_format"="html"}, requirements={"_format"="html|csv"})
 * @Method("GET")
 *  
 */
public function indexAction(Request $request, $_format)
{
    if ($request->getRequestFormat() == 'html') {
    } elseif ($request->getRequestFormat() == 'csv') {
        $request->attributes->set('filename', 'post_article.csv');
    }

    return $this->render(‘AppBudle:Hq/Post:index.´ . $_format .  ´.twig’);
}

这篇关于如何使用@Template() 引用多个树枝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆