【问题标题】:Symfony2 form variable not viewing in an included templateSymfony2 表单变量未在包含的模板中查看
【发布时间】:2014-04-21 19:19:01
【问题描述】:

有点笨拙。我正在尝试在模板中呈现表单,该模板是包含在另一个模板中的模板。

在我的控制器中:

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 中时,我仍然得到相同的错误,即变量不存在。有什么帮助吗?

【问题讨论】:

    标签: php templates symfony


    【解决方案1】:

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

    试试

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

    【讨论】:

    • 不,它仍然给我同样的错误,变量不存在。
    • 它应该可以工作。添加invsum参数后是否清除缓存?
    • @dmnptr 是的,我做到了。仍然无法识别变量。
    • 是否有路由问题?
    • 你能不能在index.html.twig中创建{{ form(invsum) }},检查变量是否存在。
    猜你喜欢
    • 2019-02-03
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多