Message-ID: <1001345705.3054.1485851801286.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3053_2130031803.1485851801286" ------=_Part_3053_2130031803.1485851801286 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html ez_render_field

ez_render_field

=20 =20

Description

ez_render_field() is a Twig helper allowing to display a Co= ntent item's Field value, taking advantage of the template block exposed by= the FieldType used.

Template blocks for built-in FieldTypes = reside in EzPublishCoreBundle.

Prototype and Arguments

ez_render_field( eZ\Publish\Core\Repository\Value= s\Content\Content content, string fieldDefinitionI= dentifier[, hash params)<= /code>

Argument name Type Description
content eZ\Publish\Core\Repository\V= alues\Content\Content Content object the displayable field belongs to.=
fieldDefinitionIdentifier string The identifier the Field is refere= nced by.
params hash

Hash of parameters that will be= passed to the template block.

By default you can pass 2 entries:

  • lang (to override the current language, m= ust be a valid locale with xxx-YY format)
  • template (to override the template to use= , see below)
  • attr (hash of HTML attributes you want to= add to the inner markup)
  • parameters (arbitrary parameters to pass = to the template block)
Some FieldTypes might expect specific entries under the=20 parameters key, like=20 the Map Location field t= ype

Override a field tem= plate block

In some cases, you may not want to use the built-in field template block= as it might not fit your markup needs. In this case, you can choose to ove= rride the template block to use by specifying your own template. You can do= this inline when calling ez_render_field(), or globally by pr= epending a field template to use by the helper.

Your template block must comply to a regular FieldType template block, <= a href=3D"/display/EZP/FieldType+template">as explained in the FieldType do= cumentation.

Inline override

You can easily use the template you need by filling the te= mplate entry in the params argument.

=20
{{ ez_render_field( 
       content, 
       'my_field_identifier',
       { 'template': 'AcmeTestBundle:fields:my_field_template.html.twig' }
   ) }}
=20

The code above will load my_field_template.html.twig locate= d in AcmeTestBundle/Resources/views/fields/.

=20
{# AcmeTestBundle/Resources/views/fields/my_field_template.html.twi=
g #}
{# Assuming "my_field_identifier" from above template example is an ezkeywo=
rd field. #}
{% block ezkeyword_field %}
    {% spaceless %}
        {% if field.value.values|length() > 0 %}
        <ul>
            {% for keyword in field.value.values %}
            <li>{{ keyword }}</li>
            {% endfor %}
        </ul>
        {% endif %}
    {% endspaceless %}
{% endblock %}
=20
Overriding a block and calling the parent

When overriding a field template block, it is possible to call the "pare= nt" one. For this, you need to import original template horizontally, using= use Twig tag.

=20
{# AcmeTestBundle/Resources/views/fields/my_field_template.html.twi=
g #}
{# Assuming "my_field_identifier" from above template example is an ezkeywo=
rd field. #}
 
{% use "EzPublishCoreBundle::content_fields.html.twig" with ezkeyword_field=
 as base_ezkeyword_field %}
 
{# Surround base block with a simple div #}
{% block ezkeyword_field %}
    <div class=3D"ezkeyword">
        {{ block("base_ezkeyword_field") }}
    </div>
{% endblock %}
=20

 

Inline overri= de using current template

Version compatibility

Inline override using current template is possible as of 5.2 / 2= 013.11

 

If you want to override a specific field template only once (i.e. becaus= e your override would be only valid in your current template), you can spec= ify the current template to be the source of the field block.

Inline override using current template
=20
{% extends "MyBundle::pagelayout.html.twig" %}

{% block content %}
    {# Note that "tags" is a field using ezkeyword fieldType #}
    <div class=3D"tags">{{ ez_render_field( content, "tags" , { "temp=
late": _self } ) }}</div>
{% endblock %}

{# Here begins the inline block for my ezkeyword field #}
{% block ezkeyword_field %}
    {% spaceless %}
        {% if field.value.values|length() > 0 %}
        <ul>
            {% for keyword in field.value.values %}
            <li>{{ keyword }}</li>
            {% endfor %}
        </ul>
        {% endif %}
    {% endspaceless %}
{% endblock %}
=20

Limitation

Using _self will only work if your current template= is extending another one.

This is basically the same limitation than for Symfony form themes.

 

Global override

In the case where you want to systematically reuse a field template inst= ead of the default one, you can append it to the field templates list to us= e by ez_render_field().

To make your template available, you must register it to the system.

ezpublish/config/ezpublish.yml
=20
ezpublish:
    system:
        my_siteaccess:
            field_templates:
                - 
                    template: "AcmeTestBundle:fields:my_field_template.html=
.twig"
                    # Priority is optional (default is 0). The higher it is=
, the higher your template gets in the list.
                    priority: 10
=20

You can define these rules in a dedicated file instead of ezpublis= h/config/ezpublish.yml. <= /span>Read the cookbook recipe= to learn more about it.

 

 

------=_Part_3053_2130031803.1485851801286--