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

Create new Template resource in Smarty 3

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
develum
Smarty n00b


Joined: 02 Dec 2009
Posts: 3

PostPosted: Wed Dec 02, 2009 2:35 am    Post subject: Create new Template resource in Smarty 3 Reply with quote

I have written the following resource to try to use VARIABLES as templates in Smarty 3, inside another PHP file I do the following:

Code:

require('smarty/libs/Smarty.class.php');
$smarty = new Smarty();
   
// template variable
$smarty->assign('tplvar',$file);
$smarty->assign('data',$data);
$smarty->assign('tplvar_time',time());
// display it
echo $smarty->fetch('var:tplvar');


In my resource file
file:smarty/libs/plugins/resource.var.php
Code:

class smarty_resource_var extends Smarty {
   
   function smarty_resource_var() {
      
   }
   
   function Smarty() {
      $this->register_resource("var", array('smarty','var_template','var_timestamp','var_secure','var_trusted'));
   }
   
   // the resource functions
   function smarty_resource_var_template($tpl_name, &$tpl_source, &$smarty_obj)
   {
      
      $tpl_source = $smarty_obj->get_template_vars($tpl_name);
        return (empty($tpl_source)) ? false : true;
   }
   
   function smarty_resource_var_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
   {
       // if var has a timestamp value, use it
       $time = $smarty_obj->get_template_vars($tpl_name.'_time');
       return (!empty($time)) ? $time : time();
   }
   
   function smarty_resource_var_secure($tpl_name, &$smarty_obj)
   {
       // assume all templates are secure
       return true;
   }
   
   function smarty_resource_var_trusted($tpl_name, &$smarty_obj)
   {
       // not used for templates
   }
   
}


But I get the following error message
Exception: Undefined Smarty method "isevaluated" in /opt/workspace/product/src/01089/www/test/smarty/libs/Smarty.class.php on line 524

Before this I didn't even get that far and the message I was receiving was undefined variable "tplvar" which I thought was before I hadn't created the resource file. But it seemed to be less critical of an error. Has anyone had any success with this and Smarty 3?

If so can you point me in the right direction, thank you![/code]
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Dec 02, 2009 9:35 pm    Post subject: Reply with quote

Smarty 3 makes use of the __construct method for initialization. If you are extending the Smarty class its constructor is not called implicitly if the your child class defines also constructor. In order to run Smarty's constructor, a call to parent::__construct() within your child constructor is required.

Code:

class MySmarty extends Smarty {
   function __construct() {
       parent::__construct();
     
       // your initislizstion code goes here

   }
}


Note also that Smarty 3 has a build in string resource. You don't need any longer a custom resource for displayig templates from variables.

Just call:
Code:

$smarty->display('string:'.$my_template_source);
Back to top
View user's profile Send private message
develum
Smarty n00b


Joined: 02 Dec 2009
Posts: 3

PostPosted: Thu Dec 10, 2009 11:56 pm    Post subject: SOLVED! Reply with quote

Great thanks for that. I will try it when we update to Smarty 3, we retroverted to 2 for our release and I got the Smarty Resource created there, but I do plan to update on the Final Release of Smarty 3 and then we'll use your suggestion, thanks. Also helpful knowing the string resource already exist. That's awesome.
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 -> Smarty 3 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