Categories > TinyButStrong general >

Reusable template block

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: mgb
Date: 2007-10-22
Time: 12:33

Reusable template block

Lets say I have this template in a separate file
includes/menu.tpl:
<table>
<tr><th>MENU</th></tr>
<tr><td><a href="/?id=[tablemenu.id;block=tr]"[tablemenu.title]</td></tr>
</table>
I would like to use this multiple times in one template like so
includes/index.tpl:
<html><head><title>INDEX</title></head><body>
<table>
<tr>
  <th>Menus</th>
  <th>Content</th>
</tr>
<tr>
  <td>
    [onload;file=includes/menu.tpl;noerr]
    [onload;file=includes/menu.tpl;noerr]
  </td>
  <td>
    lots of content here
  </td>
</tr>
</table>
</body></html>
The result should be a page with two different menu's. build on the same template.
So that I in my php could do something like this
$__tbs->MergeBlock('tablemenu',$menu0);
$__tbs->MergeBlock('tablemenu',$menu1);
is there a way to implement this in tbs?
By: TomH
Date: 2007-10-22
Time: 14:52

Re: Reusable template block

What happened when you tried that? I mean, what about it didn't work?
By: mgb
Date: 2007-10-22
Time: 15:11

Re: Reusable template block

lets say my php file looked like this
$menu0[] = array('id' => 1,'title'=>'tester1');
$menu0[] = array('id' => 2,'title'=>'tester2');
$menu0[] = array('id' => 3,'title'=>'tester3');
$menu0[] = array('id' => 4,'title'=>'tester4');

$menu1[] = array('id' => 10,'title'=>'tester10');
$menu1[] = array('id' => 20,'title'=>'tester20');
$menu1[] = array('id' => 30,'title'=>'tester30');
$menu1[] = array('id' => 40,'title'=>'tester40');

$__tbs->LoadTemplate('test.tpl');
$__tbs->MergeBlock('tablemenu',$menu0);
$__tbs->MergeBlock('tablemenu',$menu1);
$__tbs->Show();
then I would like the result to be
...
<table>
<tr><th>MENU</th></tr>
<tr><td><a href="/?id=1">tester1</a></td></tr>
<tr><td><a href="/?id=2">tester2</a></td></tr>
<tr><td><a href="/?id=3">tester3</a></td></tr>
<tr><td><a href="/?id=4">tester4</a></td></tr>
</table>

<table>
<tr><th>MENU</th></tr>
<tr><td><a href="/?id=10">tester10</a></td></tr>
<tr><td><a href="/?id=20">tester20</a></td></tr>
<tr><td><a href="/?id=30">tester30</a></td></tr>
<tr><td><a href="/?id=40">tester40</a></td></tr>
</table>
...
But I only get the first occurance of the block
...
<table>
<tr><th>MENU</th></tr>
<tr><td><a href="/?id=1">tester1</a></td></tr>
<tr><td><a href="/?id=2">tester2</a></td></tr>
<tr><td><a href="/?id=3">tester3</a></td></tr>
<tr><td><ahref="/?id=4">tester4</a></td></tr>
</table>
...
By: TomH
Date: 2007-10-22
Time: 22:30

Re: Reusable template block

I see says the blind man...

Okay -- if using two different arrays, each for a different menu...

The problem seems (to me) to be the MergeBlock() statements -- they BOTH are using the same block name! NOt going to work that way...

Try something like
$__tbs->MergeBlock('tablemenu0',$menu0);
$__tbs->MergeBlock('tablemenu1',$menu1);

And that assumes your template is something like (not tested)...
<table>
<tr>
  <th>Menu</th>
</tr>
<tr>
  <td>a href="/?[tagmenu0.id;block=tr;noerr;]=1"> [tagmenu0.title;block=tr;noerr;.] </a> </td>
  <td>
    lots of content here
  </td>
</tr>
</table>
<table>
<tr>
  <th>Menu</th>
</tr>
<tr>
  <td>a href="/?[tagmenu1.id;block=tr;noerr;]=1"> [tagmenu1.title;block=tr;noerr;.] </a> </td>
  <td>
    lots of content here
  </td>
</tr>
</table>

Hope that helps,


By: TomH
Date: 2007-10-22
Time: 22:33

Re: Reusable template block

Ooops, f course my cut and paste is fubar'ed

I should have indicated (in the template) that the blockname is "tablemenuX" not "tagmenuX"
By: Skrol29
Date: 2007-10-23
Time: 00:11

Re: Reusable template block

Hi,

The block will always have the same block name if they both come from the same subtemplate.

You can drive the merge like this:
<table>
<tr>
  <th>Menus</th>
  <th>Content</th>
</tr>
<tr>
  <td>
    [menu0;file=includes/menu.tpl;noerr]
    [menu1;file=includes/menu.tpl;noerr]
  </td>
  <td>
    lots of content here
  </td>
</tr>
</table>

PHP:
$__tbs->MergeField('menu0',''); // subtemplate is inserted once
$__tbs->MergeBlock('tablemenu',$menu0);
$__tbs->MergeField('menu1',''); // subtemplate is inserted once more
$__tbs->MergeBlock('tablemenu',$menu1);
By: mgb
Date: 2007-10-23
Time: 08:30

Re: Reusable template block

Ah, great.
Thanks to both of you.
Skrol29's solution was just what I was looking for as the blocks will be occurring a variable amount of times and different places in my template.
I think I better dive deeper into the documentation as the possibilities of tbs seems endless :)
Once again, great work!