Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

register_object in plugin

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
gerard
Smarty Regular


Joined: 18 Apr 2003
Posts: 84

PostPosted: Fri Jan 02, 2004 10:13 pm    Post subject: register_object in plugin Reply with quote

I'm trying to write a plugin that will create and register an object.

Code:
function smarty_function_get_object($params, &$smarty) {
   extract($params);

   if (empty($var)) {
      $smarty->trigger_error("assign: missing 'var' parameter");
      return;
   }

   if (empty($file)) {
      $smarty->trigger_error("assign: missing 'file' parameter");
      return;
   }

   if (empty($class)) {
      $smarty->trigger_error("assign: missing 'class' parameter");
      return;
   }

   include_once($file);

   $obj = new $class;

   $smarty->assign_by_ref($var, $obj);

   $smarty->register_object($var, $obj);
}


Code:
{get_object var="otest" class="test" file="class.test.php"}

{$otest->print_test()}
{otest->print_test}


The "{$otest->print_test()}" in the template works, but "{otest->print_test}" gives an "Fatal error: Smarty error: registered 'otest' is not an object in Smarty.class.php on line 2654" error. Any idea what the problem is? Variable scope in functions perhaps? I tried making the object global in the plugin function. Is this sort of function even possible with register_object?
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Jan 03, 2004 12:01 am    Post subject: Reply with quote

i think what you try is not possible because the object has to be registered at the time the template is compiled. that basically means it has to be registered before you call $smarty->fetch() or $smarty->display() .
Back to top
View user's profile Send private message Send e-mail Visit poster's website
gerard
Smarty Regular


Joined: 18 Apr 2003
Posts: 84

PostPosted: Sat Jan 03, 2004 1:55 am    Post subject: Reply with quote

Ah, would've have been a while before I figured that out. Is there any way to hack around it? Or, just so both object syntaxes will work in templates?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP