1
Catzwolf
Adding custom smarty plug-ins to modules without hacking the core (well almost)
  • 2010/7/24 5:57

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


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 "<?php echo "<!-- Created by Smarty! -->n"?>n".$tpl_source;
}

// register the postfilter
$xoopsTpl->register_postfilter('add_header_comment');


It is as easy as that...

Enjoy,

ATB

Catz

2
trabis
Re: Adding custom smarty plug-ins to modules without hacking the core (well almost)
  • 2010/7/24 12:04

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Those are great tips, I noticed them in the framework you are working on which is very nice!

I don't see it as a hack, the only issue is accessing the XoopsTpl 'plugins_dir' property which may become protected when we move to php5. Hopefully we will get setter methods for it.

Thank you.

3
Catzwolf
Re: Adding custom smarty plug-ins to modules without hacking the core (well almost)
  • 2010/7/24 19:30

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


Quote:

trabis wrote:
Those are great tips, I noticed them in the framework you are working on which is very nice!

I don't see it as a hack, the only issue is accessing the XoopsTpl 'plugins_dir' property which may become protected when we move to php5. Hopefully we will get setter methods for it.

Thank you.


Thanks, I have updated the Xoosla framework since I released XooslaCandy. It does make life a hell of a lot easier having some MVC to work with. I really hate having to do the same things over and over again and this does help :)

I think when the time comes to using 'protected vars and methods in Xoops within xoopstpl, then there really should be the mechanics for doing this in a proper manner. Really should have been done a long time ago.

Login

Who's Online

183 user(s) are online (106 user(s) are browsing Support Forums)


Members: 0


Guests: 183


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Mar 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits