使用 Symfony2 在 Twig 模板中渲染表单
我有一个基本的 Twig 模板,在 Twig 块的页面顶部有一个搜索栏表单。我稍后还有另一个名为“内容”的块,由我的子页面填写。目前,我的基本模板如下所示:
{% block admin_bar %}
<div id="search">
<form action="{{ path('search') }}" method="post" {{ form_enctype(search_form) }}>
{{ form_widget(search_form.term) }}
{{ form_widget(search_form.type) }}
{{ form_widget(search_form.pool) }}
{{ form_widget(search_form._token) }}
<input type="submit" value="Search" />
</form>
</div>
{% endblock %}
{% block content %}
{% endblock %}
但是,当尝试渲染子模板时,我需要传入 search_form
变量。无论如何(除了自己写出 HTML 标签)我可以避免创建这个 search_form
变量并在每次我想要渲染子视图时将其传入吗?我将 Twig 与 Symfony2 结合使用。
谢谢!
I have a base Twig template that has a search bar form in it at the top of the page in a Twig block. I have another block later on named "content" that my children pages fill out. Currently, my base template looks like this:
{% block admin_bar %}
<div id="search">
<form action="{{ path('search') }}" method="post" {{ form_enctype(search_form) }}>
{{ form_widget(search_form.term) }}
{{ form_widget(search_form.type) }}
{{ form_widget(search_form.pool) }}
{{ form_widget(search_form._token) }}
<input type="submit" value="Search" />
</form>
</div>
{% endblock %}
{% block content %}
{% endblock %}
However, when trying to render a child template I need to pass in the search_form
variable along with it. Is there anyway (short of writing out the HTML tags myself) I can avoid having to create this search_form
variable and pass it in everytime I want to render a child view? I'm using Twig in conjunction with Symfony2.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嵌入式控制器就是您所需要的。将您的 admin_bar 块放入单独的文件中:
为此模板创建控制器:
然后将控制器嵌入到原始模板中:
Embedded Controller is what you need. Put your admin_bar block into separate file:
Create controller for this template:
And then embed controller into your original template: