[SOLVED] Hiding a custom content block when it has no content?

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Locked
Daryn

[SOLVED] Hiding a custom content block when it has no content?

Post by Daryn »

I've been searching all over for this and also looking at the Smarty website, but I can't find the proper way to do this.

Basically I've created a second content block as a sidebar:

Code: Select all

{content block='sidebar'}
But when there is no content entered in it, I want it to be hidden altogether. What would be the best method of accomplishing this? I have a div wrapped around this content block and I don't want it to even be rendered in my HTML if the content block is empty.

Thanks in advance  :)
Last edited by Daryn on Mon Nov 17, 2008 4:53 pm, edited 1 time in total.
nhaack

Re: Hiding a custom content block when it has no content?

Post by nhaack »

Hi Daryin,

you could assign the content block to a smarty variable and then check if it's empty. If it has content, dispaly the contents block nd some stuff around it:

Code: Select all

{content block='sidebar' assign='sidebar_var'}
{if !empty($sidebar_var)}
<div>
{$sidebar_var}
</div>
{/if}
Best
Nils
Daryn

Re: Hiding a custom content block when it has no content?

Post by Daryn »

Hi Nils,

Thanks for the input on this. I was aware of the ability to assign a variable to a content block but didn't know about the "!empty" variable. I gave it a whirl but for some reason it doesn't display anything. I checked my source and it's not outputting anything, regardless of it containing content or not.

This is exactly what I've added to my template:

Code: Select all

{content block='sidebar' assign='sidebar_var'}
{if !empty($sidebar_var)}
<div id="sidebar">
{$sidebar_var}
</div>
{/if}
Any ideas?

Thanks again.
nhaack

Re: Hiding a custom content block when it has no content?

Post by nhaack »

Hi Daryn,

that's wierd. I checked the exact code snipped on a fresh install and it works just as it was planned.

Have you checked that the sidebar is not hidden via CSS or something?
Can you see something right where you would expect the contents in the source code of the final delivered page?
Can you post an outline of your template?
Do you have some fancy caching mechanics enabled?
Have you flushed your site cache?

Just to sort out the error... create a blank template that contains nothing but:

Code: Select all

{content}
{content block='sidebar' assign='sidebar_var'}
{if !empty($sidebar_var)}{$sidebar_var}{/if}
Then create a new page based on the template and play around with it.

Hope this could give you some tips for looking into the reason why it's not working.

Best
Nils
Daryn

Re: Hiding a custom content block when it has no content?

Post by Daryn »

Hi Nils,

I've discovered the issue. It turns out that your method works flawlessly and it does exactly what I want. The problem I was having after implementing it was that my 'Home' page wasn't retaining whatever I put into that 'sidebar' content block, so it made it look like it wasn't working. This happened to other pages also but clearing the cache resolved those. But the Home page was a different story. I would input some text, submit the page for changes and then go back to it, only to find that the text I just added had disappeared. I tried clearing the site cache on the global settings but to no avail. The Home page still wouldn't retain info in the sidebar.

For the Home page, I went into options, unchecked 'cachable' then submitted the changes, and this worked. I was then able to go back in and make it cachable again and it now allows me to set content for the sidebar block. I even tried deleting the page and re-adding it.

So now I've got

Code: Select all

{content block='sidebar' assign='sidebar_var'}
{if !empty($sidebar_var)}
<div id="sidebar">{$sidebar_var}</div>
{/if}
inside of my template and it does exactly what I need.

Thanks for your help. This is something I will re-use endlessly now.  :D
Daryn

Re: [SOLVED] Hiding a custom content block when it has no content?

Post by Daryn »

Nils,

On a side note, I just started using your Twitterfeed API function for CMSMS and it's really nice! Before, I was using the Javascript one that Twitter offers and it didn't actually inject any of the entries so it wasn't easy to format. Yours allows me to do everything I wanted to be able to do, like formatting the date/time, styling it and inserting everything individually into their own elements, etc.

Also, the Javascript Twitter API wasn't loading quickly at all so it would make my site lag. Yours loads a lot faster now since it caches the data.

Nice work!
Last edited by Daryn on Mon Nov 17, 2008 4:57 pm, edited 1 time in total.
nhaack

Re: [SOLVED] Hiding a custom content block when it has no content?

Post by nhaack »

Thanks for the flowers. Nice to hear that it works so nicely for you. Let me know if you miss any features as I plan to touch the code again soon.

Best
Nils
Daryn

Re: [SOLVED] Hiding a custom content block when it has no content?

Post by Daryn »

I think right now it pretty much covers everything I was looking for, and it integrates nicely with my CMSMS installation. I'll let you know if there's anything else I can think of. I was actually going to try and create something myself but yours does just fine.
applejack
Power Poster
Power Poster
Posts: 1014
Joined: Fri Mar 30, 2007 2:28 am

Re: [SOLVED] Hiding a custom content block when it has no content?

Post by applejack »

You don't need the !empty writing it as {if $sidebar_var} means the same or to test for negative {if !$sidebar_var}

Website Design & Production
http://www.applejack.co.uk
Daryn

Re: [SOLVED] Hiding a custom content block when it has no content?

Post by Daryn »

applejack wrote: You don't need the !empty writing it as {if $sidebar_var} means the same or to test for negative {if !$sidebar_var}
That's good to know and it's logical.  :)
Locked

Return to “CMSMS Core”