php twig include,php – symfony2 / twig:如何在表单主题中使用的块中使用include?

为了处理我的表单集合,我有一个用于块集合小部件的自定义表单主题.此块集合窗口小部件呈现为表,因此取决于block_collection_header和block_collection_body.

块集合小部件始终保持不变,但有时我自定义其他两个块,块集合头和块集合体

我的工作代码:

{# From file myview.html.twig #}

{% form_theme form ':Model:prototype_table_collection.html.twig' %}

{% form(form) %}

Ant这个表单主题如下:

{# From file ':Model:prototype_table_collection.html.twig' #}

{% block collection_widget %}

{% spaceless %}

{% if prototype is defined %}

{% set body = prototype %}

{% set attr = attr|merge({'data-prototype': block('collection_body') }) %}

{% set header = prototype %}

{% set attr = attr|merge({'data-header': block('collection_header') }) %}

{% endif %}

{% if form.vars.allow_delete is defined and form.vars.allow_delete %}

{% set allow_delete = true %}

{% else %}

{% set allow_delete = false %}

{% endif %}

{{ form_errors(form) }}

{% if form.children|length > 0 %}

{% if form.children[0]|length > 0 %}

{% set header = form.children[0] %}

{{ block('collection_header') }}

{% endif %}

{% endif %}

{% for rows in form %}

{% spaceless %}

{% if rows.children|length > 0 %}

{% set body = rows %}

{{ block('collection_body') }}

{% endif %}

{% endspaceless %}

{% endfor %}

{% if prototype is defined %}

{% if form.vars.attr['data-add_label'] is defined %}

{% set add_label = form.vars.attr['data-add_label'] ~ ' ' %}

{% else %}

{% set add_label = 'Ajouter ' %}

{% endif %}

{{ add_label }}

{% endif %}

{% endspaceless %}

{% endblock collection_widget %}

{% block collection_header %}

{% for field in header %}

{% if 'checkbox' not in field.vars.block_prefixes %}

{{ form_label(field)|raw }}

{% else %}

{% if field.vars.attr['data-label'] is defined %}

{{ field.vars.attr['data-label'] }}

{% else %}

Options

{% endif %}

{% endif %}

{% endfor %}

{% if allow_delete %}

Supprimer

{% endif %}

{% endblock %}

{% block collection_body %}

{% spaceless %}

{% set fieldNum = 1 %}

{{ form_errors(body) }}

{% for field in body %}

{{ form_widget(field) }}

{{ form_errors(field) }}

{% set fieldNum = fieldNum + 1 %}

{% endfor %}

{% if allow_delete %}

{% endif %}

{% endspaceless %}

{% endblock %}

我想使用的代码不起作用:

视图保持不变

{# From file myview.html.twig #}

{% form_theme form ':Model:prototype_table_collection.html.twig' %}

{% form(form) %}

在这里,我试图从第一个块内部化代码

{# From file ':Model:prototype_table_collection.html.twig' #}

{% block collection_widget %}

{{include(':Model:collection_widget.html.twig')}}

{%end block%}

{% block collection_header %}

{#stays the same as the previous code for this block. It is called by the block collection_widget #}

{%end block%}

{% block collection_body %}

{#stays the same as the previous code for this block. It is called by the block collection_widget #}

{%end block%}

新的外化文件:

{#From file ':Model:collection_widget.html.twig' #}

{# Here I put the same exact code as I had before inside the block collection_widget, I'm not changing the code, I'm just trying to externalize this part #}

包含不起作用,我的收藏不加载.

我试过扩展布局,它也不起作用.

示例:

{# From file ':Model:prototype_table_collection.html.twig' #}

{% extends :Model:parent.html.twig' %}

{% block content %}

{% block collection_header %}

{#stays the same as the previous code for this block. It is called by the block collection_widget #}

{%end block%}

{% block collection_body %}

{#stays the same as the previous code for this block. It is called by the block collection_widget #}

{%end block%}

{%end block%}

和父母:

{# From file ':Model:parent.html.twig' #}

{% block collection_widget %}

{# same code as brefore #}

{%end block%}

{% block content %}

{% endblock %}

如何避免在我使用它的每个表单模板中重复此{%block collection_widget%}代码?

解决方法:

Horizontal reuse is an advanced Twig feature that is hardly ever needed in regular templates. It is mainly used by projects that need to make template blocks reusable without using inheritance.

只需在主模板中包含use标记:

{# :Model:prototype_table_collection.html.twig #}

{% use ':Model:collection_widget.html.twig' %}

{% block collection_header %}

{# code #}

{%end block%}

{% block collection_body %}

{# code #}

{%end block%}

然后定义collection_widget块,就好像它首先位于prototype_table_collection.html.twig文件中一样:

{# :Model:collection_widget.html.twig #}

{% block collection_widget %}

{# code #}

{% endblock %}

标签:php,forms,symfony,twig

来源: https://codeday.me/bug/20190830/1764789.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值