[SOLVED] Show news only if news are active

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
User avatar
thomahawk
Power Poster
Power Poster
Posts: 293
Joined: Fri Jul 25, 2008 10:13 am
Location: Zug, Switzerland

[SOLVED] Show news only if news are active

Post by thomahawk »

I was surprised to discover the {news} tag does show up the summary template even without any article active.

I found an example to only show news if there are news articles active. However, this does not work:
{capture assign=news_there}{news}{/capture}
{if $news_there}
{news}
{/if}
It still shows the summary template div and news box, just without articles. But I want to not show anything with no active article.

Any other solution? Maybe an "if news active" in the template?
Last edited by thomahawk on Fri Nov 15, 2013 11:54 am, edited 1 time in total.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1922
Joined: Mon Jan 29, 2007 4:47 pm

Re: Show news only if news are active

Post by Jo Morg »

There have been a few posts about this on this forum before. To cut a long story short: It all depends on the template (in this case the summary one). The default one, and most of the customized ones, don't return empty even if there are no news to show. These usually return, at the very least, some html tags (div's etc). You need to check the template and make it return empty if there are no news.
Which goes to say that it could, and should, all be done at the summary template level and avoid that (unnecessary IMO) last check on the main page template. HTH.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Show news only if news are active

Post by velden »

http://forum.cmsmadesimple.org/viewtopi ... 20&t=68417

Check for $count variable.

Code: Select all

{if $count->value gt 0} //not sure, check if that works
User avatar
thomahawk
Power Poster
Power Poster
Posts: 293
Joined: Fri Jul 25, 2008 10:13 am
Location: Zug, Switzerland

Re: Show news only if news are active

Post by thomahawk »

Hi Velden. Thanks for your useful post.

I made the template like this:

Code: Select all

{if $count->value gt 0}
<div class="newsbox">
<div class="newsboxtitel">NEWS</div>

{foreach from=$items item=entry}
<div class="newsboxtext">
<span class="newsdate">{$entry->postdate|cms_date_format}</span><br>
{$entry->summary|cms_escape:htmlall}<br>
<a href="{$entry->moreurl}"> ZUM ARTIKEL</a></div>
{/foreach}

<div style="clear:both"></div>
</div>
{else}
Test
{/if}
From Debug, $count is available and shows "1", nevertheless nothing from this template is shown, with or without news articles active. Is something missing?

Update: Even when I make 2 articles active I see Debug shows $count ->value = 1. But I see $itemcount does react correctly and shows a value of 0 or 1 depending on the amount of activated articles, so I changed the summary code to: {if $itemcount->value gt 0}, but still the template does never display. Only the else statement "Test" shows up.

I also tried it reversed:

Code: Select all

{if $itemcount->value == 0}

{else}
<div class="newsbox">
<div class="newsboxtitel">NEWS</div>

{foreach from=$items item=entry}
<div class="newsboxtext">
<span class="newsdate">{$entry->postdate|cms_date_format}</span><br>
{$entry->summary|cms_escape:htmlall}<br>
<a href="{$entry->moreurl}"> ZUM ARTIKEL</a></div>
{/foreach}

<div style="clear:both"></div>
</div>

{/if}
With articles activated, does also not show them.

Thanks
Thom
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Show news only if news are active

Post by velden »

Stole this from another template:

Code: Select all

{if $items|@count > 0}
Didn't check it myself but it seems to make sense.
User avatar
thomahawk
Power Poster
Power Poster
Posts: 293
Joined: Fri Jul 25, 2008 10:13 am
Location: Zug, Switzerland

Re: Show news only if news are active

Post by thomahawk »

Velden. Many thanks! Thats it. Now it works. I made it like this:

Code: Select all

{if $items|@count == 0}
nada
{else}

{foreach from=$items item=entry}
{$entry->postdate|cms_date_format}<br>
{$entry->summary|cms_escape:htmlall}<br>
<a href="{$entry->moreurl}"> MORE</a>
{/foreach}

{/if}

(I really don't understand why the main cmsms news info page http://docs.cmsmadesimple.org/modules/core/news has an example for this (on the bottom of the page) which does not work.)
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: [SOLVED] Show news only if news are active

Post by Rolf »

We do have a feedback form...
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Show news only if news are active

Post by velden »

thomahawk wrote: (I really don't understand why the main cmsms news info page http://docs.cmsmadesimple.org/modules/core/news has an example for this (on the bottom of the page) which does not work.)
That example would work now probably.

Code: Select all

{capture assign=news_there}{news}{/capture}
    {if $news_there}
    <div id="news">
    <h3>News</h3>
    {news}
    </div>
    {/if}
But I don't think it's a very good example because:
- The current Sample template will always have output even if item count is zero (which makes this example useless)
- Output of {news} is captured and assigned, and if it has content {news} is called again. Unnecessary because the $news_there variable already contains what you want to show so better use {$news_there}

Example might have worked in older versions of News module with other sample templates.
User avatar
thomahawk
Power Poster
Power Poster
Posts: 293
Joined: Fri Jul 25, 2008 10:13 am
Location: Zug, Switzerland

Re: [SOLVED] Show news only if news are active

Post by thomahawk »

Strange. Someone moved this topic into the wrong forum section. This does not fit in "the lounge".
User avatar
paulbaker
Dev Team Member
Dev Team Member
Posts: 1465
Joined: Sat Apr 18, 2009 10:09 pm
Location: Maidenhead, UK
Contact:

Re: [SOLVED] Show news only if news are active

Post by paulbaker »

Agreed; moved :)
Locked

Return to “Modules/Add-Ons”