Categories > TinyButStrong general >

conditional block display

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: NM
Date: 2006-04-02
Time: 05:58

conditional block display

I'm trying to get a block to show based on the setting of a variable.
For example (below) if the php page is called from a certain page, it displays certain data from the dbase in the template; if it is called from a different page then different data is shown in the template, because a different variable is set.
Here is php
$Years=2005;
$Months=3;
$latest_issue=1;

if (isset($nav_name)){
$nav_name='about';
$TBS->MergeBlock('block1',$cnx_fruittree,'SELECT * FROM stories WHERE Years='.$Years.' AND Months='.$Months.' ORDER by Category') ;

$TBS->Show() ;
}

if (isset($latest_issue)) {
$nav_name='latest issue';
$latest_issue=2;
$TBS->MergeBlock('block2',$cnx_fruittree,'SELECT * FROM stories WHERE Years='.$Years.' AND Months='.$Months.' ORDER by Category') ;

$TBS->Show() ;
}

In the template I have this (below) but I'm not sure which tbs conditionals to use (I've tried lots but none do what I expect).
Any suggestions/hints/examples would be appreciated.
Below I assume that if the var.latest_issue is set to 2, then the correct block is shown. All I get is the number 1 or 2, instead of a block, depending on whether var.latest_issue is 1 or 2.
<table width="80%" border="0" cellspacing="2" cellpadding="0">
                            <tr>
                                <td bgcolor="#a2cba2"><strong>Category:</strong>[var.latest_issue; if [val]=2; then block2.Category; block=tr; headergrp=Category]</td>
                            </tr>
                            <tr>
                                <td>[var.latest_issue; if [val]=2; block2.Headline; block=tr;headergrp=Headline ]</td>
                            </tr>
                        </table>
By: Skrol29
Date: 2006-04-02
Time: 16:38

Re: conditional block display

Hello NM,

Parameters "if/then" are for fields and can only change the final value, not the other parameters.
Parameters "when" are for blocks and make the block displayed or not. That is what you should use in a separated TBS tag.
In the following example, I use an [onload] automatic block which is processed when the template is loaded, that is before you do MergeBlock().
<table width="80%" border="0" cellspacing="2" cellpadding="0">
  <tr>
     <td gcolor="#a2cba2">
       [onload;block=table;when [var.latest_issue]=2]
       <strong>Category:</strong>
       [block2.Category; block=tr; headergrp=Category]
     </td>
  </tr>
  <tr>
    <td>
      [block2.Headline; block=tr;headergrp=Headline]
    </td>
   </tr>
</table>
By: NM
Date: 2006-04-15
Time: 08:15

Re: conditional block display

Thanks Skrol29.
I have made the change and can understand the reasoning, but even when the "when" is met, nothing is shown (no table).
Is there something in the php that needs to be changed? Perhaps where the Mergeblock is used?
By: Skrol29
Date: 2006-04-15
Time: 19:58

Re: conditional block display

Hello,

You can try the following in order to debug : rename the [onload] TBS tag into [onloadx;....] (or whatever to not be an automatic TBS tag). This will make it to not be processed, and then you can check if the "when" expression is like you supposed after the merge of the var field.

The problem can come from the value of $latest_issue which has not the value that you assume when the template is loaded. Don't forget that [onload] tags are processed during the LoadTemplate() call.
By: Anonymous
Date: 2006-04-16
Time: 15:27

Re: conditional block display

It works when I change to onloadx, but no table shows when  I use onload.
Below is what shows in the table (which is correct; and it shows that 2=2, so the 'when' is being met).
I'm not sure why this code shows in the table though: [onloadx; block=table; when2=2]
Each line below shows in its table cell correctly.
[onloadx; block=table; when2=2] Category: Apple and Pear 
Message from the dbase
Another message from the dbase
[onloadx; block=table; when2=2] Category: Cherries 
Message from dbase
Another messagte from dbase
By: Skrol29
Date: 2006-04-16
Time: 15:45

Re: conditional block display

Hello NM,

"when2=2" is wrong, correct expression is "when 2=2" which includes a space. You should check if the space is missing in your template.
By: NM
Date: 2006-04-16
Time: 16:38

Re: conditional block display

Skrol29,
Many thanks for the quick reply. I have added the space but it has not made any difference.
Here is the template code:
<div id="layer1">
                        Add content here
                        <p><strong>Calling from:</strong>[var.nav_name]</p>
    <p></p>
    <p><strong>Year: [var.Years] Month: [var.Months] Latest issue = [var.latest_issue]</strong></p>
                        <table width="80%" border="1" cellspacing="2" cellpadding="0">
                            <tr>
                                <td bgcolor="red">[onloadx; block=table; when [var.latest_issue]=2] <strong>Category:</strong>
                                [block2.Category; block=tr; headergrp=Category]
                                </td>
                            </tr>
                            <tr bgcolor="#00ff7f">
                                <td><a href="pginfo.php?story_ID=[block2.ID]&get_story=2">[block2.Headline; block=tr; headergrp=Headline]</a></td>
                            </tr>
                        </table>
                        <p></p>
                        <p></p>
                    </div>
There is obviously something simple (like a missing space) in the template that is upsetting normal behaviour, but I am at a loss.
If I change to onload and block=tr (instead of table) a table shows but without the 'category' and it ignores the 'when' condition. I don't know if that helps.
By: Skrol29
Date: 2006-04-16
Time: 20:48

Re: conditional block display

Hello, NM,

Try the following:
  replace [onload;block=table;when [var.latest_issue]=2]
  with [onshow;block=table;when [var.latest_issue]=2]

[onload] tags are processed during the LoadTemplate() call. User should set variables used for [onload] When condition before to call this method.
While [onshow] tags are processed during the Show() call.
By: NM
Date: 2006-04-17
Time: 09:29

Re: conditional block display

Skrol29 - Problem solved, thanks.
User should set variables used for [onload] When condition before to call this method.
I thought I was doing this, see code below:
if ....
elseif (isset($latest_issue)) {
$nav_name='latest issue';
$Years=2005;
$Months=5;
$latest_issue=2;
$TBS->MergeBlock('block2',$cnx_fruittree,'SELECT * FROM stories WHERE Years='.$Years.' AND Months='.$Months.' ORDER by Category') ;
$TBS->Show() ;
}
Given the code above, how do I get it to work with onLoad instead of onShow?
By: Skrol29
Date: 2006-04-18
Time: 13:49

Re: conditional block display

Hello,

To use [onload], the line $latest_issue=2; must be before $TBS->LoadTemplate(...).
You can try someting like this:
if ....
} elseif (isset($latest_issue)) {
  $nav_name='latest issue';
  $Years=2005;
  $Months=5;
  $latest_issue=2;
}
$TBS->LoadTemplate(...);
if ($latest_issue==2) $TBS->MergeBlock('block2',$cnx_fruittree,'SELECT * FROM ...');
$TBS->Show() ;