如何创建自定义的Symfony2 Twig表单模板块 [英] How to create a custom Symfony2 Twig form template block

查看:146
本文介绍了如何创建自定义的Symfony2 Twig表单模板块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我需要一些自定义表单模板块的项目工作。不是现有块的修改版本,而是新的块。



我已经能够创建新块并获得Symfony / Twig来重新识别和使用它们,但有限制。

看起来,表单模板块有严格的命名约定。
看起来,模板名称只能包含一个下划线。下划线之前的单词似乎也有要求和/或限制。我已经能够form_工作,但没有别的。另外,如果用_widget结束块的名称,则不会抛出异常,但如果直接在页面模板中使用该块,则不会显示任何异常。我假设_widget块只能在form_blocks中使用(true?)。



我的目标是使用项目首字母(wwui)命名所有新的自定义块,对其他开发人员(以及我自己的某个地方)来说非常清楚:-)哪些标签是特定于此项目的。



这就是我已经做到的我当前的观点:

- 按照Symfony表单自定义文档中的指定创建fields.html.twig文件

- 在config.yml中的twig.form.resources中指定该表单<
- 用函数声明创建一个Twig扩展
- 在我的模板中使用我的新块

一个简单的例子:

  // TwigExtension.php 
...
public function getFunctions()
{
$ ret =
[
new \Twig_SimpleFunction('wwui_myBlock',
null,
['node_class'=&'; Symfony\Bridge\Twig\Node\\ \\SearchAndRenderBlockNode ',
'is_sage'=> ['html']]),
...
];
返回$ ret;


{#fields.html.twig#}
{%block wwui_myBlock%}
< p>测试的简单文字。< / p> ;
{%endblock wwui_myBlock%}

这会引发异常:

 在呈现模板期间抛出异常(无法呈现表单,因为以下任何块都不存在:_siteActivityQueryForm_myBlock, 













$ b

如果我将它重命名为form_byBlock,它可以正常工作。



所以,问题是:

自定义的命名要求和限制是什么



更新27-Aug-2015 09:30

一些额外的回应@lxg评论的信息:


块简单的是一段HTML / Twig代码这可以在子模板中重写。


我不认为这完全正确。一个块(至少是一个表单块)是一个被称为Twig函数的Twig片段(例如, {{form_widget(form)}} )。



我在这里特别提到了表单块。

这些函数中的一些定义在 vendor / symfony / symfony / Bridge / Twig / Extension / FormExtension.php

及其中几个( form_widget() form_errors() form_label() form_row() form_rest() form_start() form_end())由类 Symfony \ Bridge \Twig\Node\SearchAndRenderBlockNode 实现(请参阅 getFunctions() code>方法在 FormExtension.php 中)。

这些Twig片段在 vendor / symfony / symfony / src / Symfony / Bridge / Twig / Resources / views / Form / form_div_layout中定义.html.twig



我想要做的是创建具有项目特定名称的新表单块。



我已经能够创建新的自定义表单块(如上所示),但不能使用我想要使用的名称。



<

解决方案

原来, Symfony \ Bridge \Twig\Node\SearchAndRenderBlockNode()方法是限制来自于。



还有另一种方法可以让我使用我想要的名称。它是 Symfony \Bridge\Twig\Node\RenderBlockNode()


I am working on a project for which I need some custom form template blocks. Not modified versions of the existing blocks, but new blocks.

I have been able to create new blocks and get Symfony/Twig to regognize and use them, but with limitations.

It appears that there are strict naming conventions for form template blocks. It appears that template names must contain exactly one underscore. There also appear to be requirements and/or limitations on the word preceding the underscore. I have been able to get form_ to work, but nothing else. Also, if you end the name of a block with _widget, no exceptions will be thrown, but nothing will be rendered if you use the block directly in your page template. I presume that _widget blocks may only be used from within form_blocks (true?).

My goal is to name all of my new custom blocks using the project's initials (wwui) to make it very clear to other developers (and myself somewhere down the road :-) which tags are specific to this project.

This is what I have done to get to my current point:
- Create a fields.html.twig file as specified in the Symfony form customization docs
- Specify that form in twig.form.resources in config.yml
- Create a Twig extension with a function declaration - Use my new block in my templates

A simple example:

// TwigExtension.php
...
public function getFunctions()
{
  $ret = 
  [
    new \Twig_SimpleFunction( 'wwui_myBlock', 
            null,
            [ 'node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode',
'is_sage' => [ 'html' ]] ),
    ...
  ];
  return $ret;
)

{# fields.html.twig #}
{% block wwui_myBlock %}
  <p>A simple literal for testing.</p>
{% endblock wwui_myBlock %}

This will throw an exception:

An exception has been thrown during the rendering of a template ("Unable to render the form as none of the following blocks exist: "_siteActivityQueryForm_myBlock", "siteActivityQueryForm_myBlock", "form_myBlock".") in SiteBundle:Queries:activity.html.twig at line 31.

If I rename it to form_byBlock it works fine.

So, the question is:
What are the formal naming requirements and limitations for custom form blocks.


Update 27-Aug-2015 09:30
Some additional information in response to a comment by @lxg:

A block is simple a block of HTML/Twig code which can be overridden in child templates.

I don't think that's entirely accurate. A block (at least a form block) is a Twig snippet that is referenced like a Twig function (e.g., {{ form_widget( form ) }}).

I'm asking specifically about form blocks here.

A number of these functions are defined in vendor/symfony/symfony/Bridge/Twig/Extension/FormExtension.php.
and several of those (form_widget(), form_errors(), form_label(), form_row(), form_rest(), form_start(), and form_end()) are implemented by the class Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode (see the getFunctions() method in FormExtension.php).

The Twig snippets for these are defined in vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig.

What I want do to is create new form blocks with project-specific names.

I have been able to create new, custom form blocks (as shown above), but not with the names that I would like to use.

Examining the Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode() method has yielded no additional understanding.

解决方案

Turns out that the Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode() method is where the limitations are coming from.

There is another method that allows me to use the names that I want. It is Symfony\Bridge\Twig\Node\RenderBlockNode().

这篇关于如何创建自定义的Symfony2 Twig表单模板块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆