ez_render_field: comparison of v.1 vs v.2
Versions Compared
Old Version 1
changes.mady.by.user Dominika Kurek
Saved on Jul 08, 2016
compared with
New Version Current
changes.mady.by.user André Rømcke
Saved on Dec 31, 2016
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Description
ez_render_field() is a Twig helper allowing you to display a Content item's Field value, taking advantage of the template block exposed by the Field Type used.
| Info |
|---|
| Template blocks for built-in Field Types reside in EzPublishCoreBundle. |
Prototype and Arguments
ez_render_field( eZ\Publish\CoreAPI\Repository\Values\Content\Content content, string fieldDefinitionIdentifier[, hash params] )
| Argument name | Type | Description | ||
|---|---|---|---|---|
| content | eZ\Publish\CoreAPI\Repository\Values\Content\Content | Content object the displayable field belongs to. | ||
| fieldDefinitionIdentifier | string | The identifier the Field is referenced by. | ||
| params | hash | Hash of parameters that will be passed to the template block. By default you can pass 2 entries:
|
Override a field template 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 override the template block to use by specifying your own template. You can do this inline when calling ez_render_field(), or globally by prepending a field template to use by the helper.
| Info |
|---|
| Your template block must comply to a regular Field Type template block, as explained in the Field Type documentation. |
Inline override
You can easily use the template you need by filling the template entry in the params argument.
| Code Block |
|---|
{{ ez_render_field(
content,
'my_field_identifier',
{ 'template': 'AcmeTestBundle:fields:my_field_template.html.twig' }
) }}
|
The code above will load my_field_template.html.twig located in AcmeTestBundle/Resources/views/fields/.
| Code Block | ||
|---|---|---|
|
||
{# AcmeTestBundle/Resources/views/fields/my_field_template.html.twig #}
{# Assuming "my_field_identifier" from above template example is an 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 %}
|
| Tip | |||||
|---|---|---|---|---|---|
|
|||||
| When overriding a field template block, it is possible to call the "parent." For this, you need to import original template horizontally, using use Twig tag.
|
Inline override using current template
If you want to override a specific field template only once (i.e. because your override would be only valid in your current template), you can specify the current template to be the source of the field block.
| Code Block | ||||
|---|---|---|---|---|
|
||||
{% extends "MyBundle::pagelayout.html.twig" %}
{% block content %}
{# Note that "tags" is a field using ezkeyword fieldType #}
<div class="tags">{{ ez_render_field( content, "tags" , { "template": _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 %}
|
| Warning | ||
|---|---|---|
|
||
| Using _self will only work if your current template is extending another one. This is basically the same limitation as for Symfony form themes. |
Global override
In the case where you want to systematically reuse a field template instead of the default one, you can append it to the field templates list to use by ez_render_field().
| Excerpt Include | ||||||
|---|---|---|---|---|---|---|
|
Comments
Add a comment