我有sonata管理包,不知道如何根据表单模板中的字段值更改样式。
例如字段难度...难度值越高,记录行的红色应该越多...
我怎么能这么做?我花了几个小时来理解奏鸣曲中的树枝模板,但我越读这个模板,我越困惑。
模板中存在块调用,无法确定这些调用指向或来自何处。
我使用symfony 2和Sonata管理包。
谢啦
使您自己的模板扩展SonataAdminBundle: CRUD:base_edit.html.twig,然后覆盖您的管理类的$tem板
属性,或者像这样在您的管理服务声明中传递它:
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 %}
如果你不知道要扩展的块或模板,告诉我你想自定义的视图(列表、编辑、显示),但可能是所有的。
然后检索您想要的字段或任何可以使用
{%dump%}
将模板的所有变量转储到探查器或
{%dump(myVar)%}
转储特定变量
编辑
base\u template
是从包含模板名称的控制器传递的变量。
如果仔细观察,所有这些模板最终都会扩展从控制器传递的base_template变量。此变量将始终采用上述全局模板之一的值,这就是对这些文件所做的更改如何影响所有SonataAdminBundle接口。
https://sonata-project.org/bundles/admin/master/doc/reference/templates.html#crudcontroller-操作模板
如果转储base\u模板
变量,您将知道扩展了哪个模板。
对于父窗体块,只需查看上面的内容:
{%use'SonataAdminBundle:CRUD:base_edit_form.html.twig',表单为parentForm%}
此行导入SonataAdminBundle:CRUD:base\u edit\u表单。html。细枝form
块别名为parentForm
。
因此,{{block('parentForm')}
是从SonataAdminBundle:CRUD:base_edit_form呈现表单
块的调用。html。细枝