Page 1 of 1
[SOLVED] Show news only if news are active
Posted: Wed Nov 13, 2013 4:09 pm
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?
Re: Show news only if news are active
Posted: Wed Nov 13, 2013 4:29 pm
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.
Re: Show news only if news are active
Posted: Wed Nov 13, 2013 5:05 pm
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
Re: Show news only if news are active
Posted: Fri Nov 15, 2013 7:56 am
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
Re: Show news only if news are active
Posted: Fri Nov 15, 2013 11:13 am
by velden
Stole this from another template:
Didn't check it myself but it seems to make sense.
Re: Show news only if news are active
Posted: Fri Nov 15, 2013 11:53 am
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.)
Re: [SOLVED] Show news only if news are active
Posted: Fri Nov 15, 2013 12:41 pm
by Rolf
We do have a feedback form...
Re: Show news only if news are active
Posted: Fri Nov 15, 2013 1:37 pm
by velden
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.
Re: [SOLVED] Show news only if news are active
Posted: Sun Nov 17, 2013 8:36 am
by thomahawk
Strange. Someone moved this topic into the wrong forum section. This does not fit in "the lounge".
Re: [SOLVED] Show news only if news are active
Posted: Thu Nov 21, 2013 3:09 pm
by paulbaker
Agreed; moved
