Categories > TinyButStrong general >

Subtemplate with scripts

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: howman
Date: 2005-10-18
Time: 18:38

Subtemplate with scripts

Hi Skrol,

I read all the posts about subtemplates, and I read the documentation and examples, but I am still not getting something.

I created a regular page of calendar events.  It uses two files, calendar.php and tpl_calendar.php.  Calendar.php instantiates the TBS class, connects to the database, runs a query, loads the template tpl_calendar.php and shows the result.  It works fine.  Here are the two files:

    calendar.php
   
<?
//common.php calls the TBS class and DB class
include_once('../../common.php') ;

$TBS->LoadTemplate("tpl_calendar.php") ;

//get calendar events
$query = "SELECT events.event_id,
     DATE_FORMAT(event_date, '%M %d, %Y - %W') as event_date,
     TIME_FORMAT(start_time, '%r') as start_time,
     TIME_FORMAT(end_time, '%r') as end_time,
     events.title,
     events.event_type,
     events.view_level,
     events.description,
     events.school_id,
     school.name,
     school.address1,
     school.address2,
     school.city,
     school.state,
     school.zip,
     school.phone
FROM {oj events  LEFT OUTER JOIN school  ON events.school_id = school.school_id}";

$TBS->MergeBlock('blkCal1',$DB->query($query));

$TBS->Show() ;
?>
   

    tpl_calendar.php
   
<h3>2005 - 2006 Calendar</h3>
<p>[blkCal1.event_id;block=begin] </p>
<table>
  <tr>
    <td colspan="2">[blkCal1.event_date;frm=dddd mmmm d, yyyy]<br>
    [blkCal1.start_time] - [blkCal1.end_time]</td>
  </tr>
  <tr>
    <td width="40">&nbsp;</td>
    <td><p>[blkCal1.title]<br>
  <strong>[blkCal1.name]</strong> (<a href="http://maps.yahoo.com/py/maps.py?addr=[blkCal1.address1]&csz=[blkCal1.city]%2c+[blkCal1.state]%2c+[blkCal1.zip]">Map It at Yahoo! Maps</a>)<br>
  [blkCal1.address1]<br>
  [blkCal1.city], [blkCal1.state] [blkCal1.zip]</p>
      <p>Note: [blkCal1.description]<br>
      </p></td>
  </tr>
</table>
<p>[blkCal1.title;block=end]</p>

   

I have another regular page of announcements.  It work similar to the calendar page...
 
    announcements.php
   
<?
//common.php calls the TBS class and DB class
include_once('../../common.php') ;

$TBS->LoadTemplate("tpl_announcement.php") ;

//get calendar events
//get announcements
$query = 'SELECT * FROM bulletin';

$TBS->MergeBlock('blk1',$DB->query($query));

$TBS->MergeBlock('blk1',$DB->query($query));

$TBS->Show() ;
?>
   

    tpl_announcements.php
   
<h1>Announcements</h1>
<p>[blk1.subject]</p>
<p>[blk1.message]</p>

   

So now I want to create a site-wide template that has a header, footer, left panel, middle, and right.  On my home page, I want the calendar page I already wrote to appear in the middle of the page, and the announcements to appear in the right panel.  On pages that are not the home page (inside pages), I want the calendar to move to the right panel and the announcements to disappear.

I tried different uses of "onload; file=" and "onload; script=".

How do I make this work?

Thanks!
H
By: Skrol29
Date: 2005-10-19
Time: 16:49

Re: Subtemplate with scripts

Hello,

You can use several TBS Fields (one for each placement) that have all a parameter "onformat" with the same value, and also the parameter "subtpl".

Then the "onformat" function can drive the local output (boudned by the corresponding TBS Field) depending to other variables of your application, in order to output the calendar, or the annoucement list, or nothing.
To do so, don't forget that the function can access to the Field's name and other Field's parameter thanks to its arguments.
By: howman
Date: 2005-10-19
Time: 19:29

Re: Subtemplate with scripts

Hey Skrol, thanks for the reply.

Here is my site-wide template tpl_main.php (HTML tables used for illustration only):
<body>
<table border="5" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="3">Header</td>
  </tr>
  <tr>
    <td><p>Left panel</p>
      <p>[var.test;onformat=f_get_cal;subtpl]</p></td>
    <td>Center Section </td>
    <td><p>Right Panel </p>
    <p>[var.test;onformat=f_get_ann;subtpl]</p></td>
  </tr>
  <tr>
    <td colspan="3">Footer</td>
  </tr>
</table>

main.php look lilke this:
<?
//common.php calls the TBS class and DB class
include_once('../../common.php');

$test = 1;

function f_get_cal($FieldName,&$CurrVal,&$CurrPrm,&$TBS) {
    include_once("calendar.php");   
}

function f_get_ann($FieldName,&$CurrVal,&$CurrPrm,&$TBS) {
    include_once("announcement.php");   
}

$TBS->LoadTemplate("tpl_main.php") ;

$TBS->Show() ;
?>

Is this the right way to do it?

When I first tested it I got errors because calendar.php and announcement.php were calling the database and my database instance was out of scope.   You see, I use common.php to call the TBS class and the DB class.  To make my page work, I had to remove include("common.php") from both calendar.php and announcement.php and copy the DB connection function into the local script.

Is there a way I can say to calendar.php -> if this script is being called as a subtemplate, then do not call the TBS class and the DB class, if this script is called by itself, make sure to include common.php?

As always, thanks for your help.

H
By: Skrol29
Date: 2005-10-19
Time: 19:43

Re: Subtemplate with scripts

Hello,

I was describing another way, but yours is good too.
On your case :
  [onshow;script=calendar.php;subtpl]
is better than :
  [var.test;onformat=f_get_cal;subtpl]
and you dont need the "onformat" function onymore.

When you use parameter "script", you can use the variable $this which refers to the TBS calling instance. And your script "calendar.php" can test if it is called as a subtemplate by testing isset($this). That is what I do.

For example: calendar.php
<?php

  if (isset($this)) {
    $TBS = &$this // subtemplate mode
  } else {
    $TBS = new clsTinyButStrong; //
  }
  ...

?>