我有奏鸣曲管理包,但无法弄清楚如何根据表单模板中的字段值更改样式。
例如字段难度...难度值越高,记录行的颜色应该越红...
我该怎么做?我花了几个小时来理解奏鸣曲中的 Twig 模板,但是我读这个模板越多,我就越感到困惑。
模板之间存在 block 调用,并且不可能弄清楚该调用从哪里产生或从哪里产生。
我使用 symfony 2 和 Sonata Admin Bundle。
谢谢
最佳答案
制作您自己的模板,扩展 SonataAdminBundle:CRUD:base_edit.html.twig 并覆盖管理类的 $templates
属性或将其传递到您的管理服务声明中,如下所示:
librinfo_crm.admin.organism:
class: Librinfo\CRMBundle\Admin\OrganismAdmin
arguments: [~, Librinfo\CRMBundle\Entity\Organism, LibrinfoCRMBundle:OrganismAdmin]
tags:
- name: sonata.admin
manager_type: orm
group: Customers Relationship Management
label: librinfo.crm.organism_admin.label
label_translator_strategy: blast_core.label.strategy.librinfo
calls:
- [ setTemplate, [edit, LibrinfoCRMBundle:OrganismAdmin:edit.html.twig]] #set a custom edit template
- [ setTemplate, [show, LibrinfoCRMBundle:OrganismAdmin:show.html.twig]] #sets a custom show template
您的自定义模板将需要覆盖默认奏鸣曲模板,例如:
{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}
{% block form %}
{# your custom code #}
{% endblock %}
如果您不知道要扩展的 wich block 或 wich 模板,请告诉我您想要自定义的 View (列表、编辑、显示),但可能是所有 View 。
然后检索您想要的字段或任何您可以使用的内容
{% dump %}
在分析器中转储模板的所有变量或
{% dump(myVar) %}
转储特定变量
编辑
base_template
是从 Controller 传递的变量,其中包含模板的名称。
If you look closely, all of these templates ultimately extend the base_template variable that’s passed from the controller. This variable will always take the value of one of the above mentioned global templates, and this is how changes made to those files affect all the SonataAdminBundle interface.
如果您转储 base_template
变量,您就会知道扩展了哪个模板。
对于 parentForm
block ,只需查看上面的内容:
{% 使用 'SonataAdminBundle:CRUD:base_edit_form.html.twig' 并将表单作为parentForm %}
此行导入 SonataAdminBundle:CRUD:base_edit_form.html.twig form
block ,别名为 parentForm
。
因此 {{ block('parentForm') }}
是从 SonataAdminBundle:CRUD:base_edit_form.html.twig 渲染 form
block 的调用。
关于symfony - 根据字段值更改奏鸣曲管理包中的行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41898481/