Categories > TinyButStrong general >

ondata function not called in subtemplate

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: MBt
Date: 2008-05-17
Time: 12:55

ondata function not called in subtemplate

Hi,
I continue the problem I posted here http://www.tinybutstrong.com/forum.php?msg_id=8922 in a new thread to treat it as a global issue not as specific to hierarchical structure.

For new readers, here is the problem. I created a menu hierarchy script. it works fine in standalone mode but not when called as sub template. I found out that 'ondata' function is not called when it's in subtpl an I wonder what I did wrong.

Without any error nor success I tried :
1/ to declare the function in main script
2/ to declare the function in sub script
3/ to use ObjectRef and ~functionName (as in the example hereunder)
4/ to use a not existing function name (no error displayed when used as subtpl)

Main Data Class : menu.class.php
<?php
define('ROOT_IDX', 1);
$data_from_db = array(
    array ( 'idx' =>  1, 'parent' =>0, 'title' => 'Menu 1'),
    array ( 'idx' =>  2, 'parent' =>0, 'title' => 'Menu 2'),
    array ( 'idx' =>  3, 'parent' =>0, 'title' => 'Menu 3'),
    array ( 'idx' =>  4, 'parent' =>0, 'title' => 'Menu 4'),

    array ( 'idx' =>  5, 'parent' =>1, 'title' => 'Item 1.1'),
    array ( 'idx' =>  6, 'parent' =>1, 'title' => 'Item 1.2'),
    array ( 'idx' =>  7, 'parent' =>1, 'title' => 'Item 1.3'),
    array ( 'idx' =>  8, 'parent' =>2, 'title' => 'Item 2.1'),
    array ( 'idx' =>  9, 'parent' =>2, 'title' => 'Item 2.2'),
    array ( 'idx' => 10, 'parent' =>2, 'title' => 'Item 2.3'),
    array ( 'idx' => 11, 'parent' =>3, 'title' => 'Item 3.1'),
    array ( 'idx' => 12, 'parent' =>3, 'title' => 'Item 3.2'),
    array ( 'idx' => 13, 'parent' =>3, 'title' => 'Item 3.3'),
    array ( 'idx' => 14, 'parent' =>4, 'title' => 'Item 4.1'),
    array ( 'idx' => 15, 'parent' =>4, 'title' => 'Item 4.2'),
    array ( 'idx' => 16, 'parent' =>4, 'title' => 'Item 4.3'),

    array ( 'idx' => 17, 'parent' =>5, 'title' => 'Item 1.1.1'),
    array ( 'idx' => 18, 'parent' =>5, 'title' => 'Item 1.1.2'),
    array ( 'idx' => 19, 'parent' =>6, 'title' => 'Item 1.2.1'),
    array ( 'idx' => 20, 'parent' =>6, 'title' => 'Item 1.2.2'),
    array ( 'idx' => 21, 'parent' =>8, 'title' => 'Item 2.1.1'),
    array ( 'idx' => 22, 'parent' =>8, 'title' => 'Item 2.1.2'),
    array ( 'idx' => 23, 'parent' =>9, 'title' => 'Item 2.2.1'),
    array ( 'idx' => 24, 'parent' =>11, 'title' => 'Item 3.3.1'),
    array ( 'idx' => 25, 'parent' =>15, 'title' => 'Item 4.2.1'),

    array ( 'idx' => 26, 'parent' =>21, 'title' => 'Item 2.1.1.1')
);

class Menu  {
// ------------------- Properties -------------------
    var $menus;
    var $currParent;
   
   
// ------------------- Constructor -------------------
    function Menu ($rootParent = 0){
        $this->tags = array();
        $this->currParent = $rootParent;
    }
   
// ------------------- Methods -------------------
    function fetchData() {
        global $data_from_db;

        foreach ($data_from_db as $idx => $menuItem) {
            //group menuItems with parent ID
            $this->menus[$menuItem['parent']][] = $menuItem;       
        }

        //returns the root list of menuItems
        return $this->menus[$this->currParent];
    }

// ------------------- TBS data plugin -------------------
    function Menu_open(&$source,&$query) {
        $this->currParent = ($query) ; //query is %p1% where %p1% is ParentId
        return $this ;
    }

    function Menu_fetch(&$db,$num) {
        if (is_array($this->menus[$this->currParent]) && $num <= count($this->menus[$this->currParent])) {
            return $this->menus[$this->currParent][$num-1] ;
        } else {
            return false ;
        }
    }

    function Menu_close(&$db) {
        $this->currParent = 0 ;
    }
   
    function on_load_data($BlockName,&$CurrRec,$RecNum) {
        global $SrcSub;
       
        // Add the source of the Subitem tables as a new field of the current row.
        $CurrRec['subitems'] = str_replace('[root.idx]',$CurrRec['idx'],$SrcSub);
    }

}

?>

PHP source of subScript : menuTree.php
<?php

//Init TBS
if (isset($this)) {
    //Called as subtemplate of TBS
    $TBS =& $this;
    global $menuTree; //in subtpl mode data MUST be in main tpl

} else {
    //Standalone call
    require_once("menu.class.php");
    require_once("Tbs.class.php");
    $TBS = new clsTinyButStrong;
    $menuTree = new Menu();
}

$TBS->ObjectRef =& $menuTree;

//Load data
$rootMenus =& $menuTree->fetchData();

//Load template
$TBS->LoadTemplate('menuTree.html') ;
//logic can be found here : http://www.tinybutstrong.com/forum.php?msg_id=7900
// Save the source of the parentTree table
$SrcSub = $TBS->GetBlockSource('parentTree',false,false);

// Delete TBS tags in the template
$TBS->MergeField('parentTree','');

$TBS->MergeBlock('root',$rootMenus);
$TBS->MergeBlock('sub','~Menu','%p1%');

$TBS->Show() ;

?>

HTML source of subScript : menuTree.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>MenuTree sub-script</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

  <ul >
    <li >[root.title;block=li]
      <ul >
          [parentTree;block=ul]
        <li >
               [sub.title;block=li;bmagnet=ul;p1=[root.idx];ondata=~on_load_data] 
               <!-- whatever you type as ondata value, it will not raise an error when called as subtpl -->
              [sub.subitems;protect=no;htmlconv=no;comm]
        </li>
      </ul>
    </li>
  </ul>

</body>
</html>

PHP source of Main template : index.php
<?php
require_once("Tbs.class.php");
require_once("menu.class.php");

//called scripts
$scripts['menuTree'] = "menuTree.php";

//Data MUST be created in main script else it's not displayed
$menuTree = new Menu();

$TBS = new clsTinyButStrong ;

//Load template
$TBS->LoadTemplate('main.html') ;

$TBS->Show() ;

?>


HTML source of Main template : main.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>MenuTree main script</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    [var.scripts.menuTree;script=[val];subtpl;getbody]
</body>
</html>

Thanks for your help.
MBt
By: MBt
Date: 2008-05-17
Time: 13:00

Re: ondata function not called in subtemplate

Oups!
typo in clean up of my own code :

please replace
   $this->tags = array();
with
   $this->menus = array();
in constructor.

Actually this line is useless in this example!
MBt
By: Skrol29
Date: 2008-05-21
Time: 18:56

Re: ondata function not called in subtemplate

Hi Mbt,

The ondata function is called, but you cannot trace it with a simple echo() because it is running in an output redirection mode.
If you trace it with a global variable, it works.
By: MBt
Date: 2008-05-24
Time: 13:36

Re: ondata function not called in subtemplate

YESSSS, it works!

Skrol29, you are just as  wonderfull as your TBS!
My mistake is that var $SrcSub is not defined as global in menuTree.php.

here is the solution :
in file menuTree.php add this
...
if (isset($this)) {
//Called as subtemplate of TBS
    ...
    global $SrcSub; //MUST BE SET AS GLOBAL
   ...
} else {
...

Thanks for your help and this great Template Engine.
MBt