Page 1 of 1
Show News-Div only when News available
Posted: Mon Oct 26, 2009 11:34 am
by antibart
Hi there,
I always use cgsimple smarty to show different template-layouts in one project. I like to use only one template .
In this case I have a one-column-content-area for all pages instead of the startpage: there will be shown some news-teasers in a second small div and the main content-div should have a smaller width too ...
... but only when there are some news to be displayed. When there a no news to be shown, the larger one column-div should be displayed as in all other pages.
I tried out with capture:
Code: Select all
{if $cgsimple->get_root_alias() == 'home' }
{capture assign=actual}
{news}
{/capture}
{if $actual != ''}
show two smaller divs, one with {contnet}, one with {news}
{/if}
{else}
show only one bigger div and no news
{/if}
but this doesn't do anything at all .... I think I should a better way to ask for an "empty news-content" instead of !=''
Any hints? Please forgive - I am still in the "trial and error"-period in smarty coding.
---
Re: Show News-Div only when News available
Posted: Tue Oct 27, 2009 12:33 am
by Dr.CSS
I use this to tell it to not display a div if it's empty but it won't tell your css to make the other div to go wide if it is empty, that is not possible unless you do some inline styling trick...
{capture assign='test'}{content block='newsblock'}{/capture}{if !empty($test)}
{$test}
{/if}
Thanks to mr calguy1000...
Re: Show News-Div only when News available
Posted: Tue Oct 27, 2009 6:18 am
by antibart
Dr. CSS wrote:
but it won't tell your css to make the other div to go wide if it is empty, that is not possible unless you do some inline styling trick...
for this I have the "else" .. it shows two different div boxes if news are empty.
Dr. CSS wrote:
{capture assign='test'}{content block='newsblock'}{/capture}{if !empty($test)}
{$test}
{/if}
This seems to me almost the same as my way - but with a kind of detour over the content block and a another empty-rule. But is the content really empty when there is a news module tag inserted?
But thank you anyway ... I will test it.
Which I have done ... just the same: no result.
Re: Show News-Div only when News available
Posted: Tue Mar 16, 2010 7:21 pm
by tomc
But is the content really empty when there is a news module tag inserted?
While adding a "make this appear only if news exists" bit to a site, I found that the news module outputs comments regardless of whether any news items exist. So simply capturing {news} and testing emptiness didn't work so much.
I ended up testing for news existing inside the news summary template and setting a 'nonews' variable to flag when there's no news.
Code: Select all
News summary template:
{if !empty($items)}
<ul>
{foreach from=$items item=entry}
...output news entries...
{/foreach}
</ul>
{else}
{assign var='nonews' value=true}
{/if}
Then having my page template test for the flag and changing output as required:
Code: Select all
Page template:
{capture assign='mynews'}
{news number="5" summarytemplate="my-summary-template" detailpage="news"}
{/capture}
{if $nonews}
There's no news...
{else}
<h2>News</h2>
<div class="newsblock">{$mynews}</div>
{/if}
Other options might be to strip all tags/whitespace from the captured module output before testing emptiness, or to simply test against the "empty" output from the news module (although that would have to be specific to the news categories used, as that info is included inside the comment tags...and is subject to any change of output from future versions of the news module).
Re: Show News-Div only when News available
Posted: Tue Mar 16, 2010 8:44 pm
by Wishbone
^ Along the lines of the suggestion above, it seems unnecessary to have the news template trip a flag when {$items} is empty. You can put the News and the in the news template.
Code: Select all
{if !empty($items)}
<h2>News</h2>
<div class="newsblock">
{* Rest of template goes here *}
</div>
{/if}