Symfony2表单变量未在包含的模板中查看

时间:2014-04-21 19:19:01

标签: php templates symfony

有点像doozy。我试图在模板中渲染表单,模板是包含在另一个模板中的模板。

在我的控制器中:

public function searchInvSumAction(Request $request)
{

    $form = $this->createFormBuilder()
        ->add('fromdate','date', array(
                'label' => 'From: ',
                'widget' => 'single_text'
            ))
        ->add('todate','date', array(
                'label' => 'To: ',
                'widget' => 'single_text'
            ))
        ->add('generate','submit')
        ->getForm();

    return $this->render('CIRBundle:Reports:index.html.twig', array(
           'invsum' => $form->createView()
        ));

}

index.html.twig模板:

// codes........

     <li>
      <input type="radio" name="tabs" id="tab3">
      <label for="tab3" class="tab">Inventory Summary</label>
      <div id="tab-content3" class="tab-content animated fadeIn">
         {{ include('CIRBundle:Reports:invsum.html.twig') }}
      </div>
    </li>

// more codes...

并在包含的模板上调用invsum.html.twig:

{{ form(invsum) }}

我收到错误消息:

Variable "invsum" does not exist in CIRBundle:Reports:invsum.html.twig at line 4

当我尝试将表单呈现到invsum.html.twig中时,我仍然得到相同的错误,即变量不存在。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

您未将变量invsumindex.html.twig传递到invsum.html.twig

尝试

{{ include('CIRBundle:Reports:invsum.html.twig', {'invsum': invsum}) }}