[solved] if statement that checks if news items are active?

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.
Post Reply
DoctorWho
Forum Members
Forum Members
Posts: 34
Joined: Mon Jan 24, 2011 5:14 pm
Location: USA

[solved] if statement that checks if news items are active?

Post by DoctorWho »

My site currently looks like this; and I would like it to look like this. But I only want the green bar on the top to be displayed if there's any news items that are active.

So I am wondering is it possible to set an if statement that check if there's any news items that are active.
Last edited by DoctorWho on Wed Jan 11, 2012 8:27 pm, edited 1 time in total.
CMSMS: 1.9.4.3 | PHP: 5.2.12 | MySQL: 5.0.45 | Linux
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: an if statement that checks if any news items are active

Post by spcherub »

You can try something like this...

Code: Select all

{capture assign=my_news}{news}{/capture}
{if $my_news != ''}
    {eval var=$news}
{/if}
This will only show the news item(s) if there is at least one that is active.

The other option is to insert the conditional logic into the news template. Something like this will work...

Code: Select all

{if $itemcount > 0}
...code to display news items...
{/if} 
Hope this helps.
S
DoctorWho
Forum Members
Forum Members
Posts: 34
Joined: Mon Jan 24, 2011 5:14 pm
Location: USA

Re: an if statement that checks if any news items are active

Post by DoctorWho »

Thanks spcherub!

I tried it like this, but it didn't work:

Code: Select all

{capture assign=my_news}{news}{/capture}
{if $my_news != ''}
    {eval var=$news}
{/if}
So I tried it like this, and it did work:

Code: Select all

{capture assign=my_news}{news}{/capture}
{if $my_news != ''}
    {news}
{/if}
I also tried it like this (inside news summary template), and it worked as well:

Code: Select all

{if $itemcount > 0}

{foreach from=$items item=entry}

<p><strong><a href="{$entry->moreurl}">{$entry->title|cms_escape}</a></strong></p>

{/foreach}

{/if}
Is there any advantage or disadvantage placing the code in the main page template vs. placing the code in the news summary template?

Lastly, how do I get this to only appear on the home page?

Something like this:

Code: Select all

{capture assign=my_news}{news}{/capture}
{if $my_news != '' && $node->current == "home"}
<div id=#news>{news}</div>
{/if}
or would something like this work better:

Code: Select all

{capture assign=my_news}{news}{/capture}
{if $my_news != '' && $cgsimple->get_root_alias() == "home"}
<div id=#news>{news}</div>
{/if}
I know I can place this

Code: Select all

{cms_module module="news"}
in side a page, but I want this banner/marquee to appear before and outside of the main content section ( {content} )
CMSMS: 1.9.4.3 | PHP: 5.2.12 | MySQL: 5.0.45 | Linux
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: an if statement that checks if any news items are active

Post by spcherub »

The first one did not work because there was a typo (my fault). It should be as follows:

Code: Select all

{capture assign=my_news}{news}{/capture}
{if $my_news != ''}
    {eval var=$my_news}
{/if}
It is more processing efficient this way (above) because you are not calling the News module twice.

If you are using the same template for the home page and other pages, then, you should check for the page alias (similar to one of your examples), so something like this should work.

Code: Select all

{if $page_alias == "home"}
{capture assign=my_news}{news}{/capture}
{if $my_news != ''}
    <div id=#news>{eval var=$my_news}</div>
{/if}
{/if}
You could also make this more flexible by checking one of the Extra Page Attributes instead of the alias. This way you can apply this logic to amy page that has a specific value in one of the Extra Page Attributes.

Given that you want to use this logic just on the home page, it is probably better (easier to read) to place the logic in the page template instead of the news template. Someone else more familiar with the backend could shed some light on which method is more "efficient".

Hope that helps.
-S
DoctorWho
Forum Members
Forum Members
Posts: 34
Joined: Mon Jan 24, 2011 5:14 pm
Location: USA

Re: an if statement that checks if any news items are active

Post by DoctorWho »

Thanks again spcherub! ;D
CMSMS: 1.9.4.3 | PHP: 5.2.12 | MySQL: 5.0.45 | Linux
Post Reply

Return to “CMSMS Core”