Categories > TinyButStrong general >

reuse templates

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

reuse templates

I created a template, which shows data in a table.
I'd like to use this template in almost every page of my application. But the number of the tables, what i have to display, is different in each page. Of course, the content of the tables has to be different.

I think something like this:
The table template discribes the design, and the layout of a table. Each page has a template, which tells: how much table has to be dsiplayed, the order of the tables, etc

Is it possible?
By: TomH
Date: 2007-09-27
Time: 01:16

Re: reuse templates

Hello,

I use common templates with different sql queries in my TBS + ezSQL based CMS at http://tomhenry.us/tbs3/ look for "Prototyping a lightweight Content Management System" near the middle of the page

The database has the info about the page and it holds which template to use, what SQL query to use and other things like which location or column to position the content.

Placing the name of the subscript and what sql query to use in individual records in the database makes it very easy.

This approach works for ALL of the areas you see on that example..
* headers
* banners
* left col
* center body content
* right col
* footers

With just one query all of the page content elements are identified and as the page output is built, the individual subscripts prepare their own cuntent based on the sql statements from the first/main query.

Hope that helps,
TomH
By: zsom
Date: 2007-09-27
Time: 10:07

Re: reuse templates

Hello,

I visited your site, and there is a lots of usefull thing.
Is there a source code for "Prototyping a lightweight Content Management System"? I couldn't find it.

My problem is how to use the same template more than once in one page. Because the tamplate has only one block name. And I want to merge different data into it.

Here is the only solution, i can find out:
main.tpl:
<html>
  <body>
   <table>
    <tr bgcolor="green">
      <td valign="top">[blkData.val;block=td;htmlconv=no]</td>
    </tr>
   </table>
  </body>
</html>

table.tpl is template which contains a formatted table

main.php:
<?
include_once('tbs_class_php5.php');

$TBS =& new clsTinyButStrong;

$data = array();

$TBS->LoadTemplate('table.tpl');
$TBS->MergeBlock('tableData',$data1);

$data[1] = $TBS->Source;

$TBS->LoadTemplate('table.tpl');
$TBS->MergeBlock('tableData',$data2);
$data[2] = $TBS->Source;

$TBS->LoadTemplate('table.tpl');
$TBS->MergeBlock('tableData',$data3);
$data[3] = $TBS->Source;

$TBS->LoadTemplate('main.tpl');
$TBS->MergeBlock('blkData',$data);

$TBS->Show();
?>

It's working, but not so nice code. Another idea?

Thanks:
Zsombor
By: TomH
Date: 2007-09-27
Time: 14:25

Re: reuse templates

Here's the source code for the three cls on that example...

Remember that the query used for this main page also gets the name of the sunscript and the sql query statement from the database at the time of the first query.

I use 'subscripts' ( a .php for the subscript code plus a .html for the template code) so that I can pass the different sql statements that are stored in the database from the main script to the subscript.

The main page is like this...
index.html
<!-- ===    S E C T I O N  with 3 C O L S of modules -->
<tr>
<td valign=top  >

               
        <table width="99%" border="0" align="left" cellpadding="0" cellspacing="0">
        <tr>
   
<!-- use parentgrp to repeat td block on change in value of 'position'  -->
        <td class="col_[blk.position]" width="200" valign=top>
        <!-- blk.position;block=td;parentgrp=position -->         
                <table  class="[blk.position;block=td;parentgrp=position]" width="100%" cellspacing="0" cellpadding="0" border="0">

<!-- here's where the individual subscripts are called -->
<!-- blk.categurl  is the name of the subtemplate file in the db -->
<tr>
<td class="[blk.category]">
[blk.id;script=[blk.categurl];subtpl;block=table]
</td>
</tr>
                </table>     
          </td>
        </tr>
        </table>
</td>
</tr>

You can see that the above blk code allows for multiple uses of the subtemplate -- each different usage where the content is different will have a record in MySQL that holds the relevant data including the sql statement. There are several different 'styles' of subscripts based on where they are being used on the page.
In the example application you could see that he left hand column used a common template to should the different categories and then the articles in that category. Similarly a common subscript in the RHC shows the logos and links for advertisers.

The common subtemplate contentquery.php from above
<?php
error_reporting(E_ERROR);
include("./tbs_ddwork_ezsql.inc"); // haven't globalized this ;)

// now globalized $TBS instance in the above so we don't need to do...
//$TBS = new clsTinyButStrong ;  // now is global

$this->LoadTemplate("module_contentquery.html");  // use generic sidebar template

// query 'renames' the db fields for ease of use in the TBS subquery
$sql = "SELECT id AS mainid,descript AS mainlabel, intro AS maincontent, categsql AS mainsql from $main_table where publish='all' and type='module' and position='contentquery' ORDER BY sequence";

$db->get_results($sql);
$this->MergeBlock('main',$db);  // MAIN --use the subquery feature of TBS
$this->MergeBlock('sub',$db, '%p1%');  // SUB  --subquery will come from the database 

$this->Show() ;
?>

In the above you'll note "categsql AS mainsql" this is the actual query statement that will be used in the subquery. This is where the individualized data is found for populating the common template of the subscript.

Finally... here's the common subtemplate that is populated by a wide variety of different query statements -- note this id done in the "sub" block
<table class="module" width="100%" border="1" cellspacing="0" cellpadding="3">
<tr>
<th align=left class="contentquery" ><font color="#008080">
[main.mainlabel;htmlconv=no; block=table]
</font>
</th></tr>

<tr>
<td  class="contentquery" valign="top" nowrap=nowrap> &#160;
<a class="contentquery" href="[var..script_name]?id=[sub.subid;noerr]&show=id&cat=[sub.sublabel;noerr]">
[sub.sublabel; ope=max:24; block=tr;p1=[main.mainsql]]
</a>
</td>
</tr>
</table>

Hope that helps a bit more,
TomH

P.S. Some people complain subqueries cause slow pages but they just need to learn more about TBS -- I do also include TBS cache for speed up of all the pages and this makes for very fast page rendering indeed -- like in any proper website the caching pages (TBS) and caching queries (ezSQL) should be included where they maximize page response and minimize db server loads.