Show News-Div only when News available

For discussion and questions related to CMS Specific templates and stylesheets (CSS), and themes. or layout issues. This is not a place for generic "I don't know CSS issues"
Post Reply
antibart
Power Poster
Power Poster
Posts: 1162
Joined: Sun Aug 17, 2008 9:29 am

Show News-Div only when News available

Post 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.

---
Last edited by antibart on Mon Oct 26, 2009 1:13 pm, edited 1 time in total.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: Show News-Div only when News available

Post 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...
antibart
Power Poster
Power Poster
Posts: 1162
Joined: Sun Aug 17, 2008 9:29 am

Re: Show News-Div only when News available

Post 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.
Last edited by antibart on Tue Oct 27, 2009 6:30 am, edited 1 time in total.
tomc

Re: Show News-Div only when News available

Post 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).
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: Show News-Div only when News available

Post 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}
Post Reply

Return to “Layout and Design (CSS & HTML)”