Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Troubles with Smarty 3.0a and default_template_handler_func

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
monoxide
Smarty n00b


Joined: 31 Oct 2009
Posts: 4

PostPosted: Sat Oct 31, 2009 4:32 am    Post subject: Troubles with Smarty 3.0a and default_template_handler_func Reply with quote

I'm trying to use a default_template_handler to rewrite files to default templates when they aren't present in my theme. Well, that's working great, but following the example in the manual it doesn't work with Smarty 3.0.

My function is called, but SMARTY_CORE_DIR isn't defined, and I can't see core.write_file.php anywhere. How should I go about this with 3.0?

My handler looks like this currently:

Code:

function smarty_default_template_handler($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj)
{
   if ($resource_type == "file")
   {
      $pos = strpos($resource_name, '/');
      if ($pos === false)
      {
         // No directory structure, indicates we're looking for a theme file.
         // If it's not there, then we can't help.
         return false;
      }
      
      $module = substr($resource_name, 0, $pos);
      $template = substr($resource_name, $pos + 1);
      $file = '';
      
      // Load up the template file.
      if (file_exists(dirname(__FILE__).'/../modules/'.$module.'/views/'.$template))
      {
         $filename = dirname(__FILE__).'/../modules/'.$module.'/views/'.$template;
         $file = file_get_contents($filename);
      }
      elseif (file_exists(dirname(__FILE__).'/../local/modules/'.$module.'/views/'.$template))
      {
         $filename = dirname(__FILE__).'/../local/modules/'.$module.'/views/'.$template;
         $file = file_get_contents($filename);
      }
      else
      {
         // Module or view doesn't exist. We can't help.
         return false;
      }
      
      // Write out the template
      require_once SMARTY_CORE_DIR . 'core.write_file.php';
        smarty_core_write_file( array( 'filename'=>$filename, 'contents'=>$file ), $smarty_obj );
      return true;
   }
   
   // If it's not a file retrieval we can't help.
   return false;
}
Back to top
View user's profile Send private message
monoxide
Smarty n00b


Joined: 31 Oct 2009
Posts: 4

PostPosted: Sat Oct 31, 2009 5:27 am    Post subject: Reply with quote

Well, I seem to have got it working with:

Code:

      // Write out the template
      $smarty_obj->template_filepath = $filename;
      $smarty_obj->template_source = $file;
      $smarty_obj->compileTemplateSource();
      
      return true;


If anyone has any comments about whether this is the right or wrong way of doing this though, they'd be appreciated.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Oct 31, 2009 12:55 pm    Post subject: Reply with quote

The default template handler should just return the template source and a filestamp. Not doing anything else.

Code:
function smarty_default_template_handler($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj)
{
   if ($resource_type == "file")
   {
      $pos = strpos($resource_name, '/');
      if ($pos === false)
      {
         // No directory structure, indicates we're looking for a theme file.
         // If it's not there, then we can't help.
         return false;
      }
     
      $module = substr($resource_name, 0, $pos);
      $template = substr($resource_name, $pos + 1);
      $file = '';
     
      // Load up the template file.
      if (file_exists(dirname(__FILE__).'/../modules/'.$module.'/views/'.$template))
      {
         $filename = dirname(__FILE__).'/../modules/'.$module.'/views/'.$template;
         $template_source = file_get_contents($filename);
         $template_timestamp = filemtime($filename);
      }
      elseif (file_exists(dirname(__FILE__).'/../local/modules/'.$module.'/views/'.$template))
      {
         $filename = dirname(__FILE__).'/../local/modules/'.$module.'/views/'.$template;
         $template_source = file_get_contents($filename);
         $template_timestamp = filemtime($filename);
      }
      else
      {
         // Module or view doesn't exist. We can't help.
         return false;
      }
     
      return true;
   }
   
   // If it's not a file retrieval we can't help.
   return false;
}


That should work for both Smarty2 and Smarty3.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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