[Solved]Trying to Hide Empty Content Blocks

For discussion and questions related to CMS Specific templates and stylesheets (CSS), and themes. or layout issues. This is not a place for generic "I don't know CSS issues"
jasnick
Power Poster
Power Poster
Posts: 695
Joined: Sat Jan 15, 2011 8:36 am

[Solved]Trying to Hide Empty Content Blocks

Post by jasnick »

Good afternoon

In template have 4 content blocks laid out as following:

<div class="box">
{content block="second_content_block" label="Info1"}
</div>

through to "Info4".

I want to add two more boxes in case they are needed but obviously don't want them showing until they are used.

Found this on this site:

Hiding empty content blocks

If you would like to hide a content block - for example, if a content editor has not put any content in it - you can achieve this with some simple Smarty code. In our example, we want to hide a content block named SpecialOffer.

Note that for content blocks and parameter "block" you should use names without special characters or spaces. If you need a descriptive text for content block use "label" parameter.

{content block='SpecialOffer' label='Enter Special Offer' assign='offer'}

{if isset($offer)}
<div id="SpecialOffer">
{$offer}
</div>
{/if}

So I adapted this to:

<div class="box">
{content block='sixth_content_block' label='Info5' assign='offer'}
{if isset($offer)}
<div id="sixth_content_block">
{$offer}
</div>
{/if}
</div>

but don't know what to change assign='offer' to.

Because I have it wrapped in the div .box, it still shows on the site. I only want the div to show if it contains something.

Can anyone tell me how to do this please?

Thanks
Last edited by jasnick on Fri Apr 26, 2013 1:15 am, edited 1 time in total.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Trying to Hide Empty Content Blocks

Post by velden »

It seems okay what you are doing.

Maybe you could put the .box div inside the if-block:

Code: Select all

{content block='sixth_content_block' label='Info5' assign='offer'}
{if isset($offer)}
<div class="box">
<div id="sixth_content_block">
{$offer}
</div>
</div>
{/if}

Code: Select all

assign='offer'
means that the content of the content block will be assigned to a smarty variable named 'offer'. So you can and will use that later to check if that variable exists and use its value {$offer}

You can change 'offer' to any valid variable name (not already used of course).
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: Trying to Hide Empty Content Blocks

Post by Rolf »

- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
jasnick
Power Poster
Power Poster
Posts: 695
Joined: Sat Jan 15, 2011 8:36 am

Re: Trying to Hide Empty Content Blocks

Post by jasnick »

Hi Velden and Rolf

Velden - I did try various combinations yesterday before I posted to no avail

Rolf - will try as per your link this morning and let you know what happens

Thanks for the replies!
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm

Re: Trying to Hide Empty Content Blocks

Post by rotezecke »

you may also want to wrap the if statement

Code: Select all

    {nocache}
    {content block='Sidebar' assign='sidebar'}
    {if $sidebar ne ''}<div id="sidebar">{$sidebar}</div>{/if}
    {/nocache}
as documented here. http://docs.cmsmadesimple.org/general-i ... g-in-cmsms
but i believe the actual content block does not have to be wrapped. you can assign it (as Rolf suggested) at top of template.
note: the example uses
ne ''
(not equal empty) which may be better than isset (something can be set but still be empty, no? )
but at any rate, if you set the
<div class="box">
outside the if statement, you will always have it in the page.
jasnick
Power Poster
Power Poster
Posts: 695
Joined: Sat Jan 15, 2011 8:36 am

Re: Trying to Hide Empty Content Blocks

Post by jasnick »

Thanks for your input rotezecke, which I did not see until after I wrote this reply so have not implemented any of your suggestions at the moment.

I've finally got it working with one strange happening.

Added this to top of template:
{strip}
{process_pagedata}
<!-- Create a second content block! -->
{content block='second_content' assign='second_content' label='Info1'}
{content block='third_content' assign='third_content' label='Info2'}
{content block='fourth_content' assign='fourth_content' label='Info3'}
{content block='fifth_content' assign='fifth_content' label='Info4'}
{content block='sixth_content' assign='sixth_content' label='Info5'}
{content block='seventh_content' assign='seventh_content' label='Info6'}
{/strip}

Then added to template in <div id="info">info <div id="rnd_container">

{if $second_content} <!-- Only show second content block when it has some content -->
<div class="box">
{$second_content}
</div>
{/if} plus the other 5 boxes making 6 in all.

This now works as intended but with a strange result:

I have 4 content boxes with items, and 2 spares. To test, I added an h1 to the spare fifth box with an image and it shows fine. I then remove the h1 and the image (in the editing box that will be used by the client) but the outline of the box is still visible online. I then went into the html code section and the code was still there albeit empty: <h1></h1> and <p></p> . So in order to make the outline of the box disappear I had to remove the <h1></h1> and <p></p> from the html section and of course it finally disappeared. However, I would rather the client not have to do that.

Why is is still there if it is removed from the editing box?

Thanks
User avatar
paulbaker
Dev Team Member
Dev Team Member
Posts: 1465
Joined: Sat Apr 18, 2009 10:09 pm
Contact:

Re: Trying to Hide Empty Content Blocks

Post by paulbaker »

jasnick wrote:Why is is still there if it is removed from the editing box?
Because computers can't think for themselves. You and I know that they are just HTML tags, but your server just sees that there are characters and so displays the div with no visible content.
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm

Re: Trying to Hide Empty Content Blocks

Post by rotezecke »

maybe

Code: Select all

{if $second_content}
is always true as it is defined above.

Code: Select all

{if $second_content ne ''}
should remove those <div>s.
Better even to wrap in {nocache} to avoid unexpected results when caching is enabled
jasnick
Power Poster
Power Poster
Posts: 695
Joined: Sat Jan 15, 2011 8:36 am

Re: Trying to Hide Empty Content Blocks

Post by jasnick »

Hi Paul ;D I ran into something like this years ago but in reverse - my code wouldn't show until I put a <!--comment--> between the tags.

Thanks rotezecke - will try that! I forgot to do so when you mentioned it before - thanks! Will let you know how it goes.
jasnick
Power Poster
Power Poster
Posts: 695
Joined: Sat Jan 15, 2011 8:36 am

Re: Trying to Hide Empty Content Blocks

Post by jasnick »

Hi rotezecke

I haven't got this quite right. It didn't work. Taking the spare Info 5th box as an example:

Top of template:

Replaced: {content block='sixth_content' assign='sixth_content' label='Info5'}

With: {nocache}
{content block='sixth_content' assign='sixth_content' label='Info5'}
{if $sidebar ne ''}<div class="box">{$box}</div>{/if}
{/nocache}

Code in template for this:


{if $sixth_content}
<div class="box">
{$sixth_content}
</div>
{/if}

Thanks
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm

Re: Trying to Hide Empty Content Blocks

Post by rotezecke »

try

Code: Select all

{nocache}
{if $sixth_content ne ''}
<div class="box">
{$sixth_content}
</div>
{/if} 
{/nocache}
jasnick
Power Poster
Power Poster
Posts: 695
Joined: Sat Jan 15, 2011 8:36 am

Re: Trying to Hide Empty Content Blocks

Post by jasnick »

Thanks but it didn't work. The box outline remained on the page. ???
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Trying to Hide Empty Content Blocks

Post by velden »

Think you should do some 'debugging' put extra comments in your code. Print variables somewhere else and look in the source of html output.

This is so basic that it must work.

First thing to look at: what is the EXACT content of the variable, so print it somewhere between some quotes for example:

Code: Select all

sixth_content: '{$sixth_content}'
then:

Code: Select all

<pre>
{if $sixth_content}sixth_content defined{/if}
{if isset($sixth_content)}sixth_content isset{/if}
{if !empty($sixth_content)}sixth_content not empty{/if}
{if $sixth_content != ''}sixth_content not empty string{/if}
</pre>
you can always combine those checks in one if statement:

Code: Select all

{if $sixth_content && isset($sixth_content) && !empty($sixth_content) && $sixth_content != ''}sixth_content not empty string etc{/if}
although it looks to me like double double checking
jasnick
Power Poster
Power Poster
Posts: 695
Joined: Sat Jan 15, 2011 8:36 am

Re: Trying to Hide Empty Content Blocks

Post by jasnick »

Thanks velden - I don't know php which makes it all a bit difficult for me.
velden wrote: First thing to look at: what is the EXACT content of the variable, so print it somewhere between some quotes for example:

Code:
sixth_content: '{$sixth_content}'
To recap the situation:

In the template above <head>:
{content block='sixth_content' assign='sixth_content' label='Info5'}

In the body of the template:

{if $sixth_content}
<div class="box">
{$sixth_content}
</div>
{/if}
It is the <div.box> that is the problem - the h1 and image disappear from the online page leaving the <div.box> in view, and leaving <h1></h1>
<p></p>
in the admin html. The text and image is now not visible in the admin editing box.

I shall have to go over everything in the morning :(
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Trying to Hide Empty Content Blocks

Post by velden »

It's not php but smarty.

But if you say <h1></h1><p></p> is in the admin, it should also output in the website (view source).
And that is expected as the field NOT is EMPTY.

Make sure that in admin, the field is empty. And find out why there is some (default?) value in there. Have a look at Site Admin -> Page Defaults -> Content.
Post Reply

Return to “Layout and Design (CSS & HTML)”