[phplib] Object Template From: Spruce Weber (sprucely <email protected>)
Date: 09/05/00

While toying with some ideas of how to implement an application server I
decided to see how difficult it would be to extend Template to call member
functions of objects. The benefit of this is that a developer can provide a
web designer with objects that have predefined functionality. All the
designer has to do is call the object's methods from within the template
like they would a javascript object.

Here is the resulting class declared in obj_template.inc...

Class objectTemplate extends Template {
  function set_object($parent, $object) {
    global ${$object};
    if(is_object(${$object})) {
      if (!$this->loadfile($parent)) {
        $this->halt("subst: unable to load $parent.");
        return false;
      }
      $str = $this->get_var($parent);
      $reg = sprintf("/\{%s\.(.*\(.*\))\}/", preg_quote($object));
      preg_match_all($reg, $str, $arr);
      while(list($k)=each($arr[0])) {
        $method = $arr[1][$k];
        eval(sprintf("\$tmp = $%s->%s;", $object, $method));
        $this->set_var(sprintf("%s.%s", $object, $method), $tmp);
      }
    }
  }
}

Here's a sample template obj_tpl_test.inc...

<html><body>
Testing method test.hello()...<br><br>
{test.hello()} <br><br>
Testing method test.greeting(string)...<br><br>
{test.greeting("How are You?")}<br><br>
Testing method test.goodbye()...<br><br>
{test.goodbye()}<br><br>
</body></html>

Here's a class to test my template...

class test_class
{
  function hello()
  {
    return "Hello!";
  }
  function goodbye()
  {
    return "Goodbye!";
  }
  function greeting($str)
  {
    return $str;
  }
}

Here's the code to parse the template...

include($_PHPLIB["libdir"]."obj_template.inc");
$test = new test_class();
$t = new objectTemplate($_PHPLIB["appdir"], "keep");
$t->set_file("obj_tpl_test", "obj_tpl_test.tpl");
$t->set_object("obj_tpl_test", "test");
$t->pparse("content", "obj_tpl_test")

Here is the resulting output...

Testing method test.hello()...

Hello!

Testing method test.greeting("How are you?")...

How are You?

Testing method test.goodbye()...

Goodbye!

...What do you think?
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

Share information about yourself, create your own public profile at
http://profiles.msn.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-unsubscribe <email protected>
For additional commands, e-mail: phplib-help <email protected>