|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
monoxide Smarty n00b
Joined: 31 Oct 2009 Posts: 4
|
Posted: Sat Oct 31, 2009 4:32 am Post subject: Troubles with Smarty 3.0a and default_template_handler_func |
|
|
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 |
|
monoxide Smarty n00b
Joined: 31 Oct 2009 Posts: 4
|
Posted: Sat Oct 31, 2009 5:27 am Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Oct 31, 2009 12:55 pm Post subject: |
|
|
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 |
|
|
|
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
|