This site has been archived. To learn more about our current products Ibexa Content, Ibexa Experience, Ibexa Commerce head over to the Ibexa Developer Portal

eZ Community » Forums » eZ Publish 5 Platform » TWIG: checking for existance of...

TWIG: checking for existance of fields in a content object

TWIG: checking for existance of fields in a content object

Friday 13 September 2013 4:20:42 pm - 10 replies

In old templates, I regularely use this pattern to check for the existance of an attribute

         {if and( is_set( $node.data_map.short_description ), $node.data_map.short_description.content.is_empty|not )}
            <div class="attribute-short_description full">
                {attribute_view_gui attribute=$node.data_map.short_description}

In TWIG I have not yet found a similar solution. This statement throws an exception when the field is not defined.

 {% if content.getField( 'short_description' ) is defined 

Any suggestions? Thanks!

Saturday 14 September 2013 3:34:19 pm


you can try this :

 {% if(content.getField('short_description').value.xml|xmltext_to_html5 is not empty  )  %}
{% endif %} 

Monday 16 September 2013 11:17:17 am

Thanks for the suggestion. Unfortunately it does not help. Obviousely trying to access the non-existant field already throws the exception:

 <h1>An exception has been thrown during the rendering of a template </h1><h1>("Invalid field identifier 'short_description' for content #357")</h1>

Checking for the existance of a field would be vital for creating versatile templates.

I guess I have to file an issue.

Monday 16 September 2013 11:29:38 am

Hi Donat.
Please try the following:

{% if content.fields['short_description'] is defined %}
    {# Do something #}
{% endif %} 

Monday 16 September 2013 5:07:59 pm

Hi Jerôme

Thanks for the suggestion. No luck, same exception is thrown sad.gif Emoticon

Any other ideas?

Tuesday 17 September 2013 11:25:41 am

Just tested and it works perfectly. Be sure to use the array syntax.

Try to clear your cache maybe blunk.gif Emoticon

Tuesday 17 September 2013 2:43:24 pm

Hi Jerôme

Yes, it works as you indicated. The execption was thrown in a sub-request blunk.gif Emoticon

Thanks for your investigations!

Tuesday 17 September 2013 3:23:46 pm

Good happy.gif Emoticon

Just blogged about it:

Thursday 26 September 2013 5:20:24 pm

Quote from Jérôme Vieilledent :

Good happy.gif Emoticon

Just blogged about it:

Very usefull thanks but I can't manage to make it work with a date field. Any tip please?

Thursday 26 September 2013 5:30:55 pm

Hi Irma

This has nothing to do with field type, so I'm guessing you're not using it properly. Code ?

Thursday 26 September 2013 5:35:12 pm

Ok sorry I got mixed up between field existance and value in that field existence.

Modified on Thursday 03 October 2013 12:20:10 pm by Irma Vrignaud


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

36 542 Users on board!

Forums menu

Proudly Developed with from