eZ Community » Forums » eZ Publish 5 Platform » Printing raw field data in a Twig...
expandshrink

Printing raw field data in a Twig template

Printing raw field data in a Twig template

Wednesday 20 February 2013 12:31:27 am - 7 replies

I'm running in to two related limitations with using eZ Data in Twig. 

content.fields in Twig returns an multidimensional array of fields and languages, with each language holding the value to the field. Unfortunately, there doesn't seem to be a way to get the Field object itself, just the values.

This causes problems with two use cases: 

  • Directly outputting text into a tag, say title or meta description. ez_render_field will render a span around the value, and you have to go through the language (hardcoding it into the template) to solve it.
  • Working with image aliases, say for customizing a background image from the CMS using inline CSS. ez_render_field only produces an image tag, and the first parameter to ez_image_alias is field, which is unavailable. 

Is there a way around this I don't know about? If not, I'd propose and ez_field Twig function that simply returns the field object. Thoughts?

Thursday 28 February 2013 8:38:35 am

Hi Joe,

This is what I've found. I'm not sure it's the right solution.

You can find the twig template for content fields in vendeor/eZ/Bundle/EsPublishCoreBundle/Ressources/views/content_fields.html/twig

You can override this file in the parameters.yml with the following code

ezsettings.your_siteaccess.field_templates:       

- {template: YourBundle:fields:general_content_fields.html.twig, priority: 0}

Then create a general_content_fields.html.twig file in your bundle ressource that extends the eZCoreBundle

Here is the code I do for ezstring

{% extends "eZCoreBundle::content_fields.html.twig" %}

{% block ezstring_field %}
{% spaceless %}    
{% set field_value = field_value.text|default( field.value ) %}
{% if tag is defined %}       
<{{ tag }} {{ block( 'field_attributes' ) }}>{{ field_value }}</{{ tag }}>   
{% else %}
{{ field_value }}    
  {% endif %}
{% endspaceless %}
{% endblock %}

 

Then use can use in a twig template :

 {{ ez_render_field(       

content,        "your field identifier",           {               'tag': 'h1'           }       )    }}

will output <h1>[Your field value]</h1>

In case of raw data just use

{{ ez_render_field(       

content,        "your field identifier"       )    }}

Tuesday 05 March 2013 1:17:45 am

Emmanuel, thanks for the info. I was trying to find a way that didn't involve overriding the custom fields template, but I think it will be necessary to do so in some cases.

In this case, I hadn't realized that it was possible to call getField() from a twig template rather than using the fields array. For my situation, {{ content.getField('my_field_identifier') }} did the trick.

Wednesday 06 March 2013 9:06:02 am

Hi Joe

You're right about the getField method.

But in some case, it will not be enough. Say, for example, you have a ezselection fieldtype with a "Yes", "No" and "May be" possible values. getfield will only gives you the index value of the field. In that case ez_render_field is mandatory and if you're not happy with HTML code rendered, you must override the custom fields template  

Wednesday 01 May 2013 8:08:55 pm

Hi Emmanuel,

great post! I'm just looking for this problem, exactly!

 

Greetings from Germany!

Frank

Thursday 02 May 2013 8:10:02 am

Hello,

As for the image variation need you can use 

ez_image_alias(<field>,<versionInfo>,<parameter_alias>)

It will give you a variation object with his uri.

 

Cheers

Modified on Thursday 02 May 2013 8:11:10 am by Matthieu Sévère

Tuesday 02 May 2017 8:55:44 pm

I dont know if there is a {set-block} equivalent with the symfony twig version but in this case, you could use a set block arround your tags y apply the raw function to it.

Wednesday 28 June 2017 6:38:09 am

The reply has been removed because of violation of forum rules.

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from