Page 1 of 1

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

Posted: Tue Jan 10, 2012 8:23 pm
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.

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

Posted: Tue Jan 10, 2012 8:55 pm
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

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

Posted: Wed Jan 11, 2012 3:53 am
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} )

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

Posted: Wed Jan 11, 2012 11:46 am
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

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

Posted: Wed Jan 11, 2012 8:26 pm
by DoctorWho
Thanks again spcherub! ;D