变量不存在twig symfony

时间:2015-10-02 09:41:11

标签: php symfony twig

我有一个执行此操作的控制器

  /**
 * @Route("/AjaxAddQuestionForm/{section}")
 * @ParamConverter("section", class="AppBundle:Section")
 */
public function ajaxAddQuestionFormAction(Request $request, $section)
{
    $question = new Question();
    $addQuestionForm = $this->createForm(new AddQuestionType(), $question);

    return $this->render('AppBundle:Form:ajaxAddQuestionForm.html.twig', array(
      'section' => $section,
      'addAjaxQuestionForm' => $addQuestionForm->createView(),
  ));
}

ajaxAddQuestionForm.html.twig文件如下所示

{% embed 'modal.html.twig' %}
{% block labelledby %}addnewquestion{% endblock %}
{% block modalId %}addnewquestion{% endblock %}

{% block modalHead %}
{{ 'client.modal.head'| trans }}
{% endblock %}
{% block modalBody %}
{{form_start(addAjaxQuestionForm)}}

<div>
  {{form_widget(addAjaxQuestionForm.section, {value: section.id})}}
</div>

<div class="form-group">
  {{form_label(addAjaxQuestionForm.name, null, {'label_attr': {'class': 'form-label'}}) }}
  <span class="help"></span>
  <div class="controls">
    {{form_widget(addAjaxQuestionForm.name, {'attr':{'class':'form-control'}})}}
  </div>
</div>
<div class="form-group">
  {{form_label(addAjaxQuestionForm.category, null, {'label_attr': {'class': 'form-label'}}) }}
  <span class="help"></span>
  <div class="controls">
    {{form_widget(addAjaxQuestionForm.category, {'attr':{'width':'100%'}})}}
  </div>
</div>
{% endblock %}
{% block modalFooter %}
<button class="btn btn-default" data-dismiss="modal" type="button">{{'client.form.cancel'| trans}}</button>
<input class="btn btn-primary create" type="submit" value="{{'client.form.add.client'| trans}}">
{{form_end(addAjaxQuestionForm)}}
{% endblock %}
{% endembed %}

最后我有一个看起来像这样的edit.html.twig

 <div aria-hidden="true" aria-labelledby="{% block labelledby %}{% endblock %}" class="modal fade" id="{% block modalId %}{% endblock %}" role="dialog" tabindex="-1">
<div class="modal-dialog">
  <div class="modal-content">
    <div class="modal-header">

      <button aria-hidden="true" class="close" data-dismiss="modal" type="button">&times;</button>
      <h4 class="modal-title" id="myModalLabel">{% block modalHead %}{% endblock %}</h4>

    </div>
    <div class="modal-body">
      {% block modalBody %}
      {% endblock %}
    </div>
    <div class="modal-footer">
      {% block modalFooter %}
      {% endblock %}
    </div>
  </div>
</div>
</div>

{% include 'AppBundle:Form:ajaxAddQuestionForm.html.twig' %}

现在我收到以下错误:

Variable "addAjaxQuestionForm" does not exist in src/AppBundle/Resources/views/Form/ajaxAddQuestionForm.html.twig at line 9 

知道我的代码有什么问题吗?

3 个答案:

答案 0 :(得分:0)

edit.html.twig 中(假设其中存在addAjaxQuestionForm变量),您需要将其传递给附带的树枝。

{% include 'AppBundle:Form:ajaxAddQuestionForm.html.twig' with {'addAjaxQuestionForm': addAjaxQuestionForm} %}

答案 1 :(得分:0)

尝试使用

{{ render(controller('AppBundle:Section:ajaxAddQuestionFormAction',{'section':section})) }}

而不是Include,您必须将“section”参数添加到ajaxAddQuestionFormAction。

如果您没有向我们发布与您的问题有关的所有代码,我们可以为您提供更多帮助。

我希望有帮助

答案 2 :(得分:0)

您应该使用渲染控制器方法而不是包含

{{ render(controller(
        'AppBundle:Form:ajaxAddQuestionForm',
        { 'section': section }
    )) }}

在此处详细了解:http://symfony.com/doc/current/book/templating.html#embedding-controllers