Testing to see if a content block exists / contains content

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
megwoo
Forum Members
Forum Members
Posts: 11
Joined: Thu Nov 10, 2005 1:08 am

Testing to see if a content block exists / contains content

Post by megwoo »

I'm a newbie to CMS made simple, so I suspect this may be an easy question--I just couldn't find an answer in the forums...

I'm trying to display special html formatting in a template ONLY if a content block exists / contains text, so I need to figure out how to test for a content block.

My content block is named "spotlight" and I've been trying things like this:

{if isset(content block="spotlight")} {/if}
{if isset($spotlight)} {/if}
{if $spotlight} {/if}

But nothing has worked. Does anyone know how I can refer to the "spotlight" content block from within a template?

THANK YOU!!!
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Testing to see if a content block exists / contains content

Post by Ted »

These kinds of tricks won't work in 0.10.3/4 because of the way that the content tag used to be handled.  However...  0.11beta4 will allow it, since there was a big overhaul of how smarty does it's thing.

The other option is to do something like....
{capture name='contentoutput'}{content block="blah"}{/capture}
{if smarty.capture.contentoutput ne ''}{/if}
megwoo
Forum Members
Forum Members
Posts: 11
Joined: Thu Nov 10, 2005 1:08 am

Re: Testing to see if a content block exists / contains content

Post by megwoo »

Wishy, Thanks for your prompt reply!

At first I couldn't get it to work, but then I added a '$':

{if $smarty.capture.contentoutput ne ''}{/if}

It works beautifully. Thanks!
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Testing to see if a content block exists / contains content

Post by Ted »

Ah, yeah.  Sorry about that.  :)

No problem.
Dr. J

Re: Testing to see if a content block exists / contains content

Post by Dr. J »

hmm... I'm trying the same thing but it doesn't work - am I doing something wrong?

{capture name=contentoutput}
{content block='info'}
{/capture}
{if $smarty.capture.contentoutput ne ''}


{$smarty.capture.contentoutput}


{/if}


regardless of the contents of the block 'info' the section within {if}{/if} is shown.

pls help
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Testing to see if a content block exists / contains content

Post by Ted »

Make sure this is all on one line:

Code: Select all

{capture name=contentoutput}{content block='info'}{/capture}
.  It's picking up the newlines and the output will never be empty.
Dr. J

Re: Testing to see if a content block exists / contains content

Post by Dr. J »

Hi Wishy,

I'v tried putting the code in one line but the result is the same: The conditional section is shown whether there is content or not.

Btw - I'm using CMSMS 0.11.1

Cheers!
Dr. J

Re: Testing to see if a content block exists / contains content

Post by Dr. J »

Do you have any idea why it doesn't work for me?

I think it might be a caching issue. It doesn't matter if I type something in the content block or if I leave it empty. The {if} statement returns true.

I've tried using {capture name=xxx} and {capture assign=xxx}

I've also tried to add |count_characters and then change the conditional statement to

Code: Select all

{capture assign=foobar}{content block='info'}{/capture}
{if $foobar|count_characters >0}HTML TO BE SHOWN{/if}
but either way the conditional statement returns true...

pls help - I'm loosing my mind here  ???
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Testing to see if a content block exists / contains content

Post by Ted »

So, I guess the question is...  what is in $foobar?

Code: Select all

{capture assign=foobar}{content block='info'}{/capture}
Start:{$foobar}:End
Dr. J

Re: Testing to see if a content block exists / contains content

Post by Dr. J »

That's the problem...

Code: Select all

{capture assign=foobar}{content block='info'}{/capture}
Start:{$foobar}:End
{if $foobar ne ""}
	We've got content!<br>
{/if}
$foobar contains the content from the content block but "We've got content!" is apparently being displayed regardless of the content of $foobar!?

On one page it says:
Start:hello world:End
We've got content!


And on the other (empty) page it says:
Start::End
We've got content!


Am I missing something?
Jacob Hage

Re: Testing to see if a content block exists / contains content

Post by Jacob Hage »

Has anyone else experienced this problem? or am I doing something wrong?

Guess it must be either a bug or a whitespace problem...
Wishy, can you confirm whether if it's a bug or perhaps replicate my setup?

Thanks
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Testing to see if a content block exists / contains content

Post by Ted »

I don't have much time at the moment to investigate this.  Though, quickly thinking, maybe a strip modifier is in order.  Just in case there is a weird character or something in there.

Code: Select all

{capture assign=foobar}{content block='info'|strip}{/capture}
Start:{$foobar}:End
{if $foobar ne ""}
	We've got content!<br>
{/if}
fadum

Re: Testing to see if a content block exists / contains content

Post by fadum »

There seems to be lots of examples in this topic, can someone tell me what i should use in 0.11.1?

i need to display only if content exists

Thanks
fadum

Re: Testing to see if a content block exists / contains content

Post by fadum »

Ive tried all methods mentioned and allways get "True" >:(

even with strip i get:

Start::End We've got content!

Any ideas please?
fadum

Re: Testing to see if a content block exists / contains content

Post by fadum »

Does anyone know another way to achieve this?? its driving me mad

Thanks
Post Reply

Return to “CMSMS Core”