Customize Template for One Field

We already customized the template used for every field whose data type is id. But you can also go deeper, and customize the way that just one specific field is rendered.

For example, let's say we need to customize how the "full name" field is rendered. No problem: in config.yml, find User, list, fields, and change fullName to the expanded configuration. To control the template add... surprise! A template option set to, how about, _field_user_full_name.html.twig:

150 lines app/config/config.yml
... lines 1 - 80
... lines 82 - 91
... lines 93 - 140
... line 142
... lines 145 - 147
- { property: 'fullName', template: '_field_user_full_name.html.twig' }
... lines 149 - 150

Copy that name. It expects this to live in the standard easy_admin directory. Create it there!

Since this is a template for one field, it will have access to the User object as an item variable. And that makes life easy. Add if item.isScientist - that's a property on User - then add a cool graduation cap:

6 lines app/Resources/views/easy_admin/_field_user_full_name.html.twig
{% if item.isScientist %}
<i class="fa fa-graduation-cap"></i>
{% endif %}
... lines 4 - 6

Below, print the full name. To do that, you can use a value variable. Pipe it through |default('Stranger'), just in case the user doesn't have any name data:

6 lines app/Resources/views/easy_admin/_field_user_full_name.html.twig
{% if item.isScientist %}
<i class="fa fa-graduation-cap"></i>
{% endif %}
{{ value|default('Stranger') }}

Try it! Yes! We now know how to customize entire page templates - like list.html.twig, templates for a specific field type, or the template for just one field.

Time to move into forms!

Leave a comment!

  • 2018-01-09 Diego Aguiar

    Hi Macros

    An endpoint is like any other route, but usually they are hitted by an AJAX call. You can define them in any controller (choose the most convenient based on your use case), and your controllers extends from Symfony's base controller class (this is not required, but gives you a lot of free functionality)

    Have a nice day

  • 2018-01-09 macros

    could you explain me what is an endpoint, and where i need to implemente it ? do i need to extend a controle or anywhere, doesnot matter the organization

  • 2018-01-09 Victor Bocharsky

    hey Macros,

    Well, for boolean fields EasyAdminBundle has toggle buttons out of the box: . For more complex features - you'll need to do a bit of work. Look at - it's a bit similar because you also need to include a custom JS code, register some listeners, and also add an endpoint where you can write whatever logic you want :)


  • 2018-01-07 macros

    Hey thank you for the amazing tutorial,
    The idea is to edit a value even a string or a number, directy from the datatable view via ajax call? like the checkbox (true or false) button, so i will be so happy if you give me some steps to implement the idea :)

    THank you cheers,