If expression with Content block in October CMS

On October CMS I am wondering if there is a way to check if content content is free?

I put it in a post tag which I would not want to display if the block was not filled.

Something like:

{% if not empty content 'homepage/message' %}
      <div class="ui message positive home-messages">
          {% content 'homepage/message' %}
        </div>
{% endif %}

      

+3


source to share


1 answer


This doesn't answer your real question,
but I'm sure it solves your problem.

I understand that you want to delete the entire block <div>

when it is empty, i.e. when the content block is empty.

Given the html / twig code just

<div class="ui message positive home-messages">
    {% content 'homepage/message' %}
</div>

      

This can be done with jQuery.

$(document).ready(function() {
    if ( $.trim( $('div.message').text() ).length == 0 ) {
        $('div.message').remove();
    }
});

      

$('div.message')

: find the block <div>

with class "message"
$('div.message').text()

: take a copy of the attached text (removes all html tags)
$.trim( $('div.message').text() )

: trim all spaces including carriage returns and non-breaking space ( &nbsp;

)
$.trim( $('div.message').text() ).length

: Measure the length of the remaining If it is zero the block is <div>

empty (no text or empty html children -blocks), so $('div.message').remove();

: delete the whole block<div>

If you need to specify more than one class to avoid removing other empty divs with the class message

, you can bind the classes
like $('div.message.home-messages')

.

You can save the jQuery code in /themes/<theme>/assets/javascript/remove_empty.js


and then inject it into the appropriate page with



function onEnd() {
    $this->addJs('assets/javascript/remove_empty.js');
} 

      

Instead, you can add remove_empty.js

JavaScript to the list of resources at the bottom of the layout file for use on the website. Typing it as above means that it will only accompany this page.

You can use much simpler

$(document).ready(function() {
    $('div.message:empty').remove();
}); 

      

But that will require your html / twig to be all one line ...

<div class="ui message positive home-messages">{% content 'homepage/message' %}</div>

      

... otherwise the block <div>

will contain carriage returns (spaces) and therefore won't :empty

.

See also:
* Using an if statement to check if a div is empty
* How to remove empty elements from the dom using jQuery?

0


source







All Articles