By: Youri
Date: 2008-10-06
Time: 03:19
|
Conditional block display based on (un)initialized array
Well, I do not want to ask, but recently was looking for how to display block with messages from my message script. I could not solve a condition, when I did not want o display whole <div> when $msg array was empty.
Here is my solution:
<div id="result">[onshow;block=div;when [msg.#]+-0;noerr]
<p class="rsuccess">[var.result.success;magnet=p;noerr]</p>
<p class="rerror">[var.result.error;magnet=p;noerr]</p>
</div>
|
Maybe that construction (when [msg.#]+-0) could be placed to TBS Blocks part of manual. I know that this is not typical block for TBS, but it is good to know, that we can count arrays not only as data source within regular blocks, but in when construction too.
Hope this helps and saves time to someone.
|
By: Youri
Date: 2008-10-06
Time: 11:33
|
Re: Conditional block display based on (un)initialized array
I was wrong, that did not work. It works when $msg is empty, but not if it contains data. Better solution:
<div id="result">[onshow; when [var.messages;noerr]!='';block=div]
<p class="rsuccess">[var.messages.success;noerr;magnet=p;mtype=*m*]</p>
<p class="rerror">[var.messages.error;noerr;magnet=p;mtype=*m*]</p>
</div>
|
Works only when $messages has not been initialized. If was and is empty then it shows empty div.
Is it possible to modify condition after onshow so it works with initialized/uninitialized array ( $messages in this case )? Thanks.
|
By: Skrol29
Date: 2008-10-06
Time: 22:48
|
Re: Conditional block display based on (un)initialized array
Hi,
Try with
[var.messages;ope=list;noerr]
|
It works only with an empty value only if $messages is an empty array.
This is a trick which matches well with your problem, but the empty() PHP function is too elaborated to be manageable at the HTML side.
|
By: Youri
Date: 2008-10-07
Time: 02:47
|
Re: Conditional block display based on (un)initialized array
Hi, could you be more explicit? I do not understand how to use your suggestion. My scenario is: when $messages is uninitialized or empty - show nothing, hide whole div. When I have for ex.
$messages['success'] with value "anystring";
|
then I want to show div and paragraph with class rsuccess and hide p with class rerror.
|
By: Skrol29
Date: 2008-10-07
Time: 22:10
|
Re: Conditional block display based on (un)initialized array
Hi,
Just replace
with
[var.messages;ope=list;noerr]
|
This should work, but this is a kind of luck.
What you need is something quite specific for a Template Engine, and that is usually solved at the PHP side.
Like this for example:
PHP:
$message_ok = (empty($message)) ? 0 : 1;
HTML:
[onshow;when [var.messages_ok]=1;block=div]
|
|
By: Youri
Date: 2008-10-08
Time: 03:37
|
Re: Conditional block display based on (un)initialized array
Thanks for your effort. Will stay with my solution. Do not want to play with tricks ;-) . PHP solution is obvious, but I did not want to initialize new variable just because of this.
|
|
Posting in progress.
Please wait...
|