eZ Community » Forums » eZ Publish 5 Platform » About ez_is_field_empty...
expandshrink

About ez_is_field_empty...

About ez_is_field_empty...

Wednesday 29 January 2014 1:04:14 pm - 4 replies

From the demo bundle:

 

    {% if not ez_is_field_empty( content, "caption" ) %}
        {{ ez_render_field( content, "caption" ) }}
    {% endif %}

 

Templates become bloated quickly with these sorts of statements...

Could this be simplified to something like this:

    {{ ez_render_field( content, "caption" ) | default(' All Rights Reserved') }}

What do you think?

Wednesday 29 January 2014 1:09:03 pm

I don't think two ways are equivalent. Imho, the first one check if field is empty and if not, then it does the ez_render_field part.

But the second one, will show always something, either the value in fhe field or the default one.

Am i wrong? 

Wednesday 29 January 2014 1:34:40 pm

If you want it to do nothing then do:

{{ ez_render_field( content, "caption" ) | default(' ') }}

Maybe semantically it is less correct, but it's more dry and templates are cleaner.

Wednesday 29 January 2014 1:42:39 pm

Quote from Gareth Arnott :

If you want it to do nothing then do:

{{ ez_render_field( content, "caption" ) | default(' ') }}

Maybe semantically it is less correct, but it's more dry and templates are cleaner.

Won't that output an space where we don't really don't want output anything?

Thinking in this example...

{% if not ez_is_field_empty( content, "caption" ) %}
<div class="caption">        {{ ez_render_field( content, "caption" ) }}
</div>{% endif %}

vs 

<div class="caption">{{ ez_render_field( content, "caption" ) | default(' ') }}</div>

Again, first won't output anything while second one will. 

As DemoBundle is one of the recommendations to learn ez5, i'd maitain the if stuff so developers can see how that thing can be done

Just my two cents. happy.gif Emoticon

Wednesday 29 January 2014 2:07:17 pm

It seems the 'default' filter in twig can be simplified further still:

{{ ez_render_field( content, "caption" ) | default }}

Would mean: "If 'caption' does not exist, do nothing"

imo this would be a great addition to the templating, but I'm not sure if ez_render_field() could be modified to support the functionality.

expandshrink

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

36 542 Users on board!

Forums menu

Proudly Developed with from