Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Customize Template for One Field

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $8.00

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:

... lines 1 - 80
easy_admin:
... lines 82 - 91
entities:
... lines 93 - 117

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:

{% 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:

{% 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!

4
Login or Register to join the conversation

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,

Reply

hey Macros,

Well, for boolean fields EasyAdminBundle has toggle buttons out of the box: https://symfony.com/doc/cur... . For more complex features - you'll need to do a bit of work. Look at https://knpuniversity.com/s... - 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 :)

Cheers!

4 Reply

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

3 Reply

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

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial is built on an older version of Symfony & EasyAdminBundle. Many of the concepts are the same, but you can expect major differences in newer versions.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.3.*", // v3.3.18
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.10.3
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.5
        "symfony/swiftmailer-bundle": "^2.3", // v2.6.7
        "symfony/monolog-bundle": "^2.8", // v2.12.1
        "symfony/polyfill-apcu": "^1.0", // v1.17.0
        "sensio/distribution-bundle": "^5.0", // v5.0.25
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.29
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.4
        "knplabs/knp-markdown-bundle": "^1.4", // 1.7.1
        "doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
        "stof/doctrine-extensions-bundle": "^1.2", // v1.3.0
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "javiereguiluz/easyadmin-bundle": "^1.16" // v1.17.21
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.7
        "symfony/phpunit-bridge": "^3.0", // v3.4.40
        "nelmio/alice": "^2.1", // v2.3.5
        "doctrine/doctrine-fixtures-bundle": "^2.3" // v2.4.1
    }
}
userVoice