Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

I think there's some confusion about terminology: A block is simply a chunk of HTML/Twig code which can be overridden in child templates. Could it be that you're refering to a macro, a template function / filter or even a custom tag? Please elaborate on what you're trying to achieve with your "blocks", maybe there's a way to solve it. – lxg Aug 27, 2015 at 7:46 I've edited your question title. Don't add tags like [SOLVED] into it, just make your own answer and accept it. Thank you :) – DonCallisto Aug 27, 2015 at 19:10

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().

One up for that. This is nowhere in the docs, tutorials or anywhere. I am dumbfounded that you have to write 3X as much code to add a simple form element. – eggmatters Nov 17, 2016 at 15:29

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.