Categories > TinyButStrong general >

php execution + conditional

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: burden
Date: 2004-03-17
Time: 09:52

php execution + conditional

Here i go again. i'm still under the impression that this is over kill, but i'm sticking with it because i've come too far in this site's development =)

so what i'm doing now is the conditionals... i have a pretty complex menu system so i wrote a php function to generate the code for me. so i just pass it 3 arguments and it creates the 18 lines of code needed. and all that goes into "blocks.inc" here's an example ofwhat's in blocks.inc:

<?
function(){

}
?>

<div>
Text:<br />
<?php
    echo childMenu("Tutorials","main.php?page=tutorials","tutorial");
    echo childMenu("Books","main.php?page=books", "books");
?>
[tbs_check.1;block=div;if[var.page]=text]
</div>
<div>
Projects<br />
<?php
    echo childMenu("Websites","main.php?page=proj_sites", "web");
    echo childMenu("Applications", "main.php?page=proj_apps", "apps");
    echo childMenu("Tutorials","main.php?page=proj_tutorials","tut");
?>
[tbs_check.1;block=div;if[var.page]=projects]
</div>

then in the template i have [tbs_include.onshow;file='inc/blocks.inc';script=[val];getob]
which doesn't work...

hopefully i explained okay.

i'm really just used to having php and including html from there. now i'm dealing with a centralized php script handling a ?page= request for each page. which includes the html, which in itself includes more php. this is really confusing for me. so my question is how do i include that php script into my template and a) still have it execute the php and b) utilize the conditional displays?
By: burden
Date: 2004-03-17
Time: 10:49

Re: php execution + conditional

so simplifying things, i did away all together with the file inclusion, and php, just to get it to work. and it still doesn't work... based on the page it should show a different block. but it doesn't.
here's what it looks like:
[tbs_check.1;block=div;if[var.page]=projects]
with tbs_check.1 what's the 1 for? is that just an id? does it need to be declared anywhere else? cause when i use else, it always just shows the else. i tried putting projects in quotes, and all. grrr.
               
By: RwD
Date: 2004-03-17
Time: 19:01

Re: php execution + conditional

well, the last question I can answer I think ;)
the 1 is to seperate tbs_checks from eachother. so if you have
[tbs_check.1;if (blah)]
[tbs_check.1;else (blah)]
Then tbs knows they balong to eachother. I think you may use anything as ids without declaring...

--------------------------------------------------

I think you original problem has something to to with
[tbs_include.onshow;file='inc/blocks.inc';script=[val];getob]

there you specify to include a file, where I think it should say script:
[tbs_include.onshow;script='inc/blocks.inc';getob]

--------------------------------------------------

But the way you do this is indeed overkill, or just not the easiest way :S
You use childMenu("Websites","main.php?page=proj_sites", "web");
to generate parts of you menu you say. Well, that is exactly what you should let tbs do, that's what it is good at.

Can you show the function code for childmenu?

--------------------------------------------------

You should have ALL html code made by tbs, or at least as much as possible or else there is no benefit in doing it, or at least less. That's where the overkill comes in ;)

--------------------------------------------------

www.metalpics.ch
I made that in the last few months, development time in total was two weeks, including correcting the images and imagenames. I am currently still updating it, adding new sections, or correcting little things (see the changelog ;P)

but my point; you can make such sites, and a lot more complex with tbs easily. If I had made it without then it would have been a complete mess, mixture of php and html, like in your example!!
?>

<div>
Text:<br />
<?php
That for example should not be in a php file when you use templates

I will wait for your response and see how it goes. I cannot offer too much help, I need some time for myself ;)
By: burden
Date: 2004-03-17
Time: 19:55

Re: php execution + conditional

jebus... everything is going wrong. i dunno what i'm doing wrong! all i want is this conditional to at least function before getting into the hard stuff. you asked for the function. here it is. nothing special.

function childMenu($caption, $url, $id){
$ret = "";

$ret .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
$ret .= " <tr>";
$ret .= "  <td bgcolor=\"#505050\" ";
$ret .= "  onmouseover=\"this.style.backgroundColor='#666666';$id.style.backgroundColor='#2f2f2f'\"";
$ret .= "  onmouseout=\"this.style.backgroundColor='';$id.style.backgroundColor=''\">";
$ret .= "    <table border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"140\">";
$ret .= "     <tr>";
$ret .= "      <td bgcolor=\"#333333\" height=\"15\" id=\"$id\"";
$ret .= "      onClick=\"window.location.href='$url'\" style=\"padding-left: 6px\">";     
$ret .= "      <a href=\"$url\"";
$ret .= "      onMouseOver=\"window.status='$caption - \'nuff said'; return true;\"";
$ret .= "      onMouseout=\"window.status=''; return true;\">$caption</a></td>";
$ret .= "     </tr>";
$ret .= "    </table>";
$ret .= "   </td>";
$ret .= "  </tr>";
$ret .= " </table>";
$ret .= "<img src=\"http://burden.cc/pics/space.gif\" height=\"5\" width=\"100\" alt=\"\"><br />";
return $ret;

}

i saw in the example section about using functions through tbs but i can't get it to work. in fact. i can't even get a simple block to compile anymore. it seems to be stuck. as if not working. no joke. in the template i have:

<div>
<br />
[vertnav.val;block=div]
</div>

and in the php i have:

$navi_array = array('test', 'test2', 'test3', 'test4', 'test5');
$TBS->MergeBlock('vertnav', $navi_array) ;

... is there something i'm missing? and that's a simplified array. i just keep downgrading. i do [var.page] and it'll tell me the page (that was passed via main.php?page=this). so i know it's doing something. but if i try anything else. no way. the conditional that i was using won't even work. based on $page it should determine which set of links to show. but it doesn't work. and i'm very sure i'm doing it right. if anyone has yahoo messenger, or aol instant messenger please IM me and help. i'm so sure that i'm doing this right it angers me. my sn for both is burdn
By: RwD
Date: 2004-03-18
Time: 19:30

Re: php execution + conditional

So, I changed your thing into something that looks better, it is not the way I would do it, but it keeps close to what you showed...

In total I used 4 files, and I didn't bother making a directory strucure for template files and other stuff. So if you want to try, then use these four files + the tbs class file (tbs-class.php v1.94) all in the same directory...

index.php
<?php
error_reporting(E_ERROR);
//****************
//*    Include needed Classes
require( 'tbs-class.php' );
//****************

//****************
//*    Template Engine object creation
$tbs         = new clsTinyButStrong;
$tbs->Render     = TBS_OUTPUT;
//****************

//****************
//*    Build the array for the menu
if ( $page == 'projects' )
{
    $PageName         = 'Projects';
   
    //****************
    //*    Build array parts
    $t_array['caption']     = 'Websites';
    $t_array['url']         = 'main.php?page=proj_sites';
    $t_array['id']         = 'web';
    //*    Insert part into array
    $menu_array[]         = $t_array;
   
    $t_array['caption']     = 'Applications';
    $t_array['url']         = 'main.php?page=proj_apps';
    $t_array['id']         = 'apps';
    $menu_array[]         = $t_array;
   
    $t_array['caption']     = 'Tutorials';
    $t_array['url']         = 'main.php?page=proj_tutorials';
    $t_array['id']         = 'tut';
    $menu_array[]         = $t_array;
    //****************
}
elseif ( $page == 'text' )
{
    $PageName     = 'Text:';
   
    //****************
    //*    Build array parts
    $t_array['caption']     = 'Tutorials';
    $t_array['url']         = 'main.php?page=tutorials';
    $t_array['id']         = 'tutorial';
    //*    Insert part into array
    $menu_array[]         = $t_array;
   
    $t_array['caption']     = 'Books';
    $t_array['url']         = 'main.php?page=books';
    $t_array['id']         = 'books';
    $menu_array[]         = $t_array;
    //****************
}
else
{
//!!!!!!!!!!!!!!!!
//! When a page doesn't exist
    $PageName     = '????';
   
    //!!!!!!!!!!!!!!!!
    //!    Build array parts
    $t_array['caption']     = 'Main page';
    $t_array['url']         = 'main.php?page=main';
    $t_array['id']         = 'tutorial';
    //!    Insert part into array
    $menu_array[]         = $t_array;
//!!!!!!!!!!!!!!!!
}
//****************

//****************
//*    load the template
$tbs->LoadTemplate( 'index.htm' );
$tbs->Show();
//****************
?>


index.htm
<html>
<body>
    <!-- call the script -->
    [tbs_include.onload;script='blocks.php']
</body>
<html>


blocks.php
<?php
//****************
//*    Need to be able to access these variables
global $tbs, $PageName, $menu_array;
//****************

//****************
//*    load the template
$tbs->LoadTemplate( 'block.htm' );
//*    Merge the data into the template
$tbs->MergeBlock( 'menu', $menu_array );
//****************
?>


block.htm
<div>
[var.PageName]<br />
<span>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
    <td bgcolor="#505050" onmouseover="this.style.backgroundColor='#666666'; [menu.id;block=span].style.backgroundColor='#2f2f2f'" onmouseout="this.style.backgroundColor='';[menu.id].style.backgroundColor=''">
        <table border="0" cellpadding="0" cellspacing="1" width="140">
        <tr>
            <td bgcolor="#333333" height="15" id="[menu.id]" onClick="window.location.href='[menu.url]'" style="padding-left: 6px">
                <a href="[menu.url]" onMouseOver="window.status='[menu.caption] - \'nuff said'; return true;" onMouseout="window.status=''; return true;">[menu.caption]</a>
            </td>
        </tr>
        </table>
    </td>
</tr>
</table>
<img src="http://burden.cc/pics/space.gif" height="5" width="100" alt=""><br />
</span>
</div>




There is a little 'but' to this script:
This will make the menu overwrite the index.htm code. Because the LoadTemplate function seems to erase the other data....

What you should do for this example is:
- replace "[tbs_include.onshow;script='blocks.php']" (in index.htm) with the contents of block.htm
- place "$tbs->MergeBlock( 'menu', $menu_array );" directly after "$tbs->LoadTemplate( 'index.htm' );" (index.php)

That will fix that...


----------------------------------
Note to Skrol
Please try the thing I described above, but do not fix as I describe!
In my tbs version 1.94 the first tables attributes get screwed up
it says <table border 0"> instead of what it should
By: burden
Date: 2004-03-19
Time: 13:09

Re: php execution + conditional

oh my... good deal. thank you for that bit of information. i haven't tried it yet, but i totally get it now. slowly grasping the concept of this whole templating engine. with your site did you just simply have one huge template file with conditionals? oh speaking of conditionals i still can't get that condition to work... i tried just [var.page] and it, indeed, printed out the page variable. so anyways,  i tried again:

just static text for testing purpose here
[tbs_check.1;block=div;if[var.page]=projects]

more mock text that would be a menu here
[tbs_check.1;block=div;if[var.page]=leech]

so on so forth, and it never displays anything unless i put an else clause. in which case it just shows that one. now in the example page there's no mergeblock statement, and i'm wondering if that just might be a typo? also in the manual it doesn't have the mergeblock so i dunno maybe that's just how it's done.  not even this will work for me though. which i'm thinking could be why i'm having so much trouble. maybe it's not just me and it's something with the script anyways. here's the code that won't work:
<div>
<br />
[vertnav.val;block=div]
</div>

and in the php i have:

$navi_array = array('test', 'test2', 'test3', 'test4', 'test5');
$TBS->MergeBlock('vertnav', $navi_array) ;

i'm wondering if there could be any special php settings i might need to have? bah i'm stumped at this point. that example above should work! it's so simple. FRUSTRATION.
By: RwD
Date: 2004-03-19
Time: 15:15

Re: php execution + conditional

The site I showed ofcourse has many smaller templates, you cannot properly ovesee a project if everything is in one file
I have stuff like 'about.htm' (http://www.metalpics.ch/Metalpics/user/templates/about.htm) which has the content of the about box
and so on, this way you can reuse blocks (like the visitorscounter on most pages)

Basically I have one site.htm, having the header, the menu, and three columns, then the footer. and each of the parts I named finds the template it needs based on variables past in the URL or session vars

I did such a conditional once:
<td width="100"  valign="center" align="center" nowrap class="wsback1">
<!-- choice 1/4: both image and thumb -->
    [tbs_check.a3-[lst.article_id];if '[var.img_diplay]' = '3';block=td]
    <a href="/data/article/images/[lst.image;htmlconv=no;friendb=a]" target="_blank">
    <img src="/data/article/images/thumbnails/[lst.image_thumb;htmlconv=no;friendb=img]" border="0">
    </a>
</td>
<td width="100" valign="center" align="center" nowrap class="wsback1">
<!-- choice 2/4: only an image -->
    [tbs_check.a2-[lst.article_id];if '[var.img_diplay]' = '2';block=td]
    <a href="/data/article/images/[lst.image;htmlconv=no;friend=a]" target="_blank" class="fade art_links">
    <img src="/user/images/store/camera.gif" border="0" alt="View larger image">
    </a>
</td>
<td width="100"  valign="center" align="center" nowrap class="wsback1">
<!-- choice 3/4: only a thumb -->
    [tbs_check.a1-[lst.article_id];if '[var.img_diplay]' = '1';block=td]
    <img src="/data/article/images/thumbnails/[lst.image_thumb;htmlconv=no;friendb=img]" border="0">
</td>
<td width="100" valign="top" nowrap class="wsback1">
<!-- choice 4/4: no image at all -->
    [tbs_check.a0-[lst.article_id];if '[var.img_diplay]' = '0';block=td]
    <font class="InactiveLink">
    No&nbsp;image
    </font>
</td>
Hopefully it shows what you need to do, I'm ill at the moment, so I can't really concentrate at the moment.


and that other problem:
<div>
<br />
[vertnav.value;block=div]
</div>

and in the php i have:

$navi_array[]['value'] = 'test';
$navi_array[]['value'] = 'test2';
$navi_array[]['value'] = 'test3';
$navi_array[]['value'] = 'test4';
$navi_array[]['value'] = 'test5';
$TBS->MergeBlock('vertnav', $navi_array) ;
I think that should work, like I said I am ill, but I think this has something to do with at which level in the array tbs looks, the first level are the records ($nav_array[x]) second level are the values ($nav_array[x]['value'])

I hope I helped
By: RwD
Date: 2004-03-19
Time: 15:22

Re: php execution + conditional

btw, I think the problem with your conditional is that there is no space following the if...

so it should be
just static text for testing purpose here
[tbs_check.1;block=div;if [var.page]=projects]

more mock text that would be a menu here
[tbs_check.1;block=div;if [var.page]=leech]
By: burden
Date: 2004-03-21
Time: 03:09

Re: php execution + conditional

omg . hahahahaha it was simply a space after if. roflmao. thanks for the help! i really appreciate it, and i hope you get well soon.