I have read Twig Template Form Function and Variable Reference on the Symfony website. However, I don't quite understand the main different between them.
For example,
{{ form(form)}} and {{ form_widget(form) }}
So, my questions are:
How different between two of these, do they generate different HTML code? and when is one preferred over the other?
form(form) will generate the HTML for the form label, the form input, and also the form errors for all of the form elements.
form_widget(form.x) will only generate the HTML for the form input(ex. textbox), so you would have to generate the others yourself, or also include form_label(form.x) and form_error(form.x)
{{ form(form)}} is generate whole form with <form></form> tags and them fields at once. {{ form_widget(form) }} is used for generate specific field separately. Also read the docs about Symfony forms and Rendering each Field by Hand
Related
I am trying to create my first Drupal 8 theme. On the frontpage, I want to list all articles with the title, summary, date posted, etc.
I more or less achieved this using views. Basically {{ page.content }} in twig, but found it to be really inflexible and I didn't really get the results that I wanted. What I want to do is just to iterate through all the articles and print each section of that article "manually". For instance:
{% for page in pages %}
{{ page.content.title }}
{{ page.content.datePosted }}
{{ page.content.body }}
{% endfor %}
So that I can have more control of what is happening and not making a lot of configuration in the views module when deploying. What is the best soltuion to achieve this? Thanks!
I recommend to use Views. Configure you view (filter by content type, etc.), you have a lot of parameters to achieve what you need.
Set the view to show "Content" and give it a view mode (you can use the default teaser, full, or create your own view mode). Then you can create a custom template for this :
node--xxxx--teaser.html.twig
In order to know how to name your custom template file, enable Twig Debugging in sites/default/services.yml. Set the debug variable to true. And clear cache. Then, you will see in source code the template name suggestion like this :
<!-- FILE NAME SUGGESTIONS:
* node--1--full.html.twig
* node--1.html.twig
x node--oeuvre--full.html.twig
* node--oeuvre.html.twig
* node--full.html.twig
* node.html.twig
-->
In your twig, you can do that kind of templating :
{{ content.my_field_image[0] }}
<h2>{{ node.title.value }}</h2>
<p>{{ content.body }}</p>
I have overridden the registration .twig files and it works except when the user doesn't enter matching passwords. How do I display any errors that are thrown by the registration process?
You can render your form errors by using form_errors function, like this:
{{ form_errors(form) }}
{{ form_errors(form.someField) }}
You should read this official documentation How to Customize Form Rendering to understand in deep how Symfony rendering your form view and how to custom the template as you want.
I am not sure, how to implement quite simple text field, into the configured form template. I have configured individual fields at yml file.
But what if I want, in the middle of the form, some text formatted informations (fetching data from another entities).
I have learned how to make custom edit template for the entities, by overriding "app/Resources/views/easy_admin/Entity/edit.html.twig".
But it allows me to change the template only around the form fields. The actual form is rendered by "{{ form(form) }}".
So, I need to edit this form() creation or customize the template somehow.
Is the only solution for this custom text field, to create custom form field type in Symfony? Or are there other methods to achieve this?
hope this helps
in the .twig file:
{% for name in value %}
<li>
<span>{{ include('#EasyAdmin/default/edit.html.twig', { value: name.name }) }}</span>
</li>
{% endfor %}
it means that the variable $name (collection) entered from the entity should be displayed inside this "for loop"
you can use the same technique to parse your desired html content
[edit.html.twig is located in app/Resources/views/easy_admin]
I'm using SonataBlockBundle in my current project.
I create a SimpleBlock and render it in my twig template with something like
{{ sonata_block_render('name' : 'myBlock') }}.
But the block consists of title and body, is there a way to render title and body separately, like I can do with form fields?
Thank you in anticipation!
The best way to go there would be to create a custom BlockService (see http://sonata-project.org/bundles/block/master/doc/reference/your_first_block.html for instructions about that), with a custom twig template where you will be able to specify your title/body rendering.
I am (still) trying to introduce http://xoxco.com/clickable/jquery-tags-input into a dedicated bundle. As far, I have a type as a child of text and a data transformer that converts comma-separated strings into arrays of Objects and vice versa.
Now I want to decorate the text field with the JQuery code linked above. As far as I understand, I have to define a block like
{% block manytomanycomboselector_widget %}
{% spaceless %}
{{ block('text_widget') }}
<script>
$(function(){
$("#{{ id }}").tagsInput();
});
</script>
{% endspaceless %}
{% endblock manytomanycomboselector_widget %}
in [MyTypeBundle]Resources/views/Form/fields.html.twig
Now, both the documentation and the answers for this question at StackOverflow state that I have to reference fields.html.twig somewhere either in the template that uses the form or in app/, but this doesn't seem to be necessarily for other field-type bundles, though I cannot see in their code why.
What do I have to configure inside the bundle besides this block in this file?
Also I didn't get where I have to put the css and js requirements for the header and how I deal with general requirements like jQuery itself.
I have the same issue & I resolve it by merging my field template in the twig.form.resources parameter.
So, in the DI extension of my bundle (MyBundle/DependencyInjection/MyBundleExtension.php), I add:
$container->setParameter('twig.form.resources', array_merge(
array('MyBundle:Form:field_widget.html.twig'),
$container->getParameter('twig.form.resources')
));
Be aware, your bundle must be registered after the TwigBundle in your AppKernel.
EDIT:
A form field is not linked to any JS or CSS. So, IMO, you have 2 solutions.
Firstly, you directly wrap your JS & CSS in your field template and your bundle stays stand-alone.
Secondly, you instruct final users that they need to include manually some JSS & CSS each time they use your field type.
The IoFormBundle & GenemuFormBundle uses the second solution like explain in their documentation.