Smarty has been in Xoops since around 2003-4 when version 2 came out and that was almost 7 years ago and still after all that time there is no way to add your own smarty plug-ins directly to the core... until now.....
Yes, I have always wanted to create different plug-ins for my modules, but that means getting the users to move them, write code to move them hoping you don't over write other ones and waiting for the core to write a way of adding them.
Well, I finally figured out a way to do it without hacking at the core, but it does require a small modification and it does come with some risks. It's not perfect, but it should work.
First to let you understand how smart and Xoops work together.
Xoops uses a class classed xoopsTpl to call smarty and configure some required variables.
When a page is rendered, a file called 'header.php' is called and doing this runs an instance of xoopsTpl and makes it ready for use.
Once the header.php file has been run, Xoops will include the next part which will be the main body of the script, this could be your frontpage or a module and this is when most of the page information is created.
Once the main body information has been created, Xoops will finally include a file called, 'footer.php' and this is when all the nice stuff is done. Xoops will gather all that information and then create the templates that are finally shown on your screen. This is when smarty is used and where we can take advantage of the smarty plug-ins, pre and post loaders to change or add other information we want via our module.
Now how do we do this? There are a couple of ways,
1. Create new Smarty plugins
2. Create pre and post loaders.
1. The Smarty plug-in.
We don't want to make life hard for ourself so first, create a couple of new folders in our module folder.
Quote:
'/modules/mymodule/plugins/smarty'
Now once we have done that, lets create out smarty plug-in and then save it to our new folder. As below we have our very basic new smarty plug-in.
function smarty_function_xoShowTime($params, &$smarty)
{
$time = time( $params );
return "echo $time;";
}
so now we should have in our path:
Quote:
'modules/mymodule/plugins/smarty/function.xoShowTime.php'
Now the last part. We need to added our path to the smart plug-in path and we need to add a line directly after the include XOOPS_ROOT_PATH . '/header.php'; in our code:
like this:
$xoopsOption['template_main'] = 'template.html';
include XOOPS_ROOT_PATH . '/header.php';
/* New line of code */
$xoopsTpl->plugins_dir[] = XOOPS_ROOT_PATH.'/modules/mymodule/plugins/smarty';
This will tell smarty to look in our new path as well as keeping the old ones. Now, this is the 'HACK' part of this and if done wrong could cause some problems. This is not perfect, but it will allow module developers to add their own smarty plug-ins.
2. Pre and post filters.
here is an example directly taken from smarty. It will let you understand how easy it is to do a pre and post filter on the templates.
function remove_dw_comments($tpl_source, &$smarty)
{
return preg_replace("//U",'',$tpl_source);
}
// register the prefilter
$xoopsTpl->register_prefilter('remove_dw_comments');
Post filter:
function add_header_comment($tpl_source, &$smarty)
{
return "echo "n"; ?>n".$tpl_source;
}
// register the postfilter
$xoopsTpl->register_postfilter('add_header_comment');
It is as easy as that...
Enjoy,
ATB
Catz