Unexpected token "name" of value "with" ("end of statement block" expected) in

在symfony2.3之后,不能使用 
{% render "AcmeDemoBundle:Demo:fancy" with { 'name': name, 'color': 'green' } %}
应该使用 {{render(controller('AcmeDemoBundle:Demo:fancy',{ 'name': name, 'color': 'green'}))}}

包含控制器的输出

那么如何在模板里嵌入另外的控制器(controller)的输出?在开发Ajax应用,或者被包含的模板引用了主模板里并不存在的变量时,这个特性就会变得十分有用。

假设你已经创建了一个fancy动作方法(action),打算将其输出包含在index模板里,可以通过使用render标签来实现:

{# src/Acme/DemoBundle/Resources/views/Demo/index.html.twig #}
{% render "AcmeDemoBundle:Demo:fancy" with { 'name': name, 'color': 'green' } %}

AcmeDemoBundle:Demo:fancy字符串指代的是Demo控制器的fancy动作方法。namecolor此时就代替了请求参数,用来执行对fancyAction的调用。

// src/Acme/DemoBundle/Controller/DemoController.php

class DemoController extends Controller
{
    public function fancyAction($name, $color)
    {
        // create some object, based on the $color variable
        $object = ...;

        return $this->render('AcmeDemoBundle:Demo:fancy.html.twig', array(
            'name' => $name,
            'object' => $object
        ));
    }

    // ...
}
在symfony2.3之后,不能使用 
{% render "AcmeDemoBundle:Demo:fancy" with { 'name': name, 'color': 'green' } %}
应该使用 {{render(controller('AcmeDemoBundle:Demo:fancy',{ 'name': name, 'color': 'green'}))}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值