1

I have a Custom Layout Plugin at times will have no content in a certain section. How do I get twig to test if a section has content inside of it before Drupal8 renders the template.

The twig if statement that does not work is: {% if content.content_left %}

This is the entire twig layout

<div class="content-top">
        {{ content.top }}
    </div>
    <div>
        <article{{ attributes.addClass('threecol-home row padded-layout colored-background') }}>
            {{ title_suffix.contextual_links }}
            <div class="col-md-12 content">
                <div class="node-title">
                    {{ content.content_top.node_title }}
                    {% if content.content_top.field_sub_title|render|trim is not empty %}
                        <span>{{content.content_top.field_sub_title}}</span>
                    {% endif%}
                </div>
                {{content.content_top|without('field_sub_title','node_title') }}
            </div>
            <div class="row">
                {% if content.content_left %}<!-- This statement will not work -->
                    <div class="col-md-3">
                        {{ content.content_left }}
                    </div>
                    <div class="col-md-6">
                        {{ content.content_body }}
                    </div>
                {% else %}   
                    <div class="col-md-9">
                        {{ content.content_body }}
                    </div>
                {% endif %}    
                <div class="col-md-3">
                    {{ content.content_right }}
                </div>
            </div>
        </article>
    </div>
    <div class="col-md-12">
        {{ content.bottom }}
    </div>
  • what is that content_left ? . is that field in some content type ? and you are working in node.html.twig right ? – logeshvaran Aug 16 '17 at 7:23
  • Custom Layout Plugin display mode created in the layouts.ym in the theme – purencool Aug 16 '17 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.