问题
I'm using Symfony 4.1.1 and Sonata Admin Bundle 3.35.2.
I want to use a custom template for a field in an admin's list view. The template is ignored. I am using Twig as my templating engine.
In the admin:
# /src/Admin/ImageAdmin.php
protected function configureListFields(ListMapper $listMapper) {
$listMapper
->add('filename', 'string', ['template' => 'list_image.html.twig'])
;
}
The template:
# /templates/list_image.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<img src="{{ value }}" style="width: 200px" />
{% endblock %}
回答1:
Should be
# /templates/list_image.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
<img src="{{ object.filename }}" style="width: 200px" />
</div>
{% endblock %}
SRC will be just filename - not full path for file, so image will not be printed. Fix that problem also.
The other problem is that, you accesed some mystical value? I don't see where you assign value to it.
You can access getters of object by writing object.fieldname. This one works as printing getter function of your current object.
回答2:
I've had same problem (Symfony 4.1), try solution from here Use custom column in Sonata Admin list so change:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
to:
{% extends '@SonataAdmin/CRUD/base_list_field.html.twig' %}
it did work for me. The problem is that even if your location is right (i got to it after some experiments) and you extend the wrong template you wont get any error.
来源:https://stackoverflow.com/questions/51108308/sonata-admin-list-field-template-is-ignored