When Smarty was first introduced into Xoops, I heard many people say they didn't like it because you can't use PHP directly within the templates. While this is true, there are however a few workarounds that will allow you to basically use PHP native function, as is they are smarty variables.
Lets take one example, the use of sprintf('ba'l, _CONSTANT); is something that developers take advantage of everyday in their code, but there has been times where I really wanted to do this in smarty and yes, you can do it without having to code a new plug-in.
Example:
<{'Hey %s, The time is: %s'|sprintf:$name:$time}>
Smarty has many useful built in smarty function as well, and we can combine both PHP and these functions to help keep our presentation smoother or nicer or more logical, which ever way you want to see it.
Another issue that has bugged me for a long time is having to create select menus in PHP and then added them afterwards. This to me is basically doing the work twice. Here is an example of creating a pull-down select menu in smarty.
First the PHP part:
$myMenu = array( 1 => 'Cats', 2 => 'wolf', 3 => 'dog', 4 => 'mice', 5 => 'men', 6 => 'women' );
$xoopsTpl->assign( 'selected', 1 );
$xoopsTpl->assign( 'mymenu', $myMenu );
Smarty:
http://www.smarty.net/manual/en/language.function.html.options.php <{html_options name=active id=active onchange=document.myform.submit(); options=$mymenu selected=$selected}>
The end result:
<select size="1" name="active" id="active" onchange="document.myform.submit();">
<option value="0" selected="selected">Catsoption>
<option value="1">wolfoption>
<option value="2">dogoption>
<option value="4">miceoption>
<option value="5">menoption>
<option value="6">womenoption>
select>
Now here is an example of this, without using $xoopsTpl->assign();
We all know that sometimes we want to do this, but do not have access to the core files or a smarty plug-in to do it. So why not do the whole load from the smarty template (and at this point I just heard Trabis scream NOOOOOO!! lol).
What we need to do is manipulate the smarty variables to add what we want and then we can access the smart variable like any other one.
First we need to tell smarty that we're playing around with PHP and not smarty, so we use the PHP smarty tag
<{php}><{/php}>
Adding out new PHP code and assigning some smarty templates:
<{php}>
$this->_tpl_vars['mymenu'] = array( 1 => 'Cats', 2 => 'wolf', 3 => 'dog', 4 => 'mice', 5 => 'men', 6 => 'women' );
$this->_tpl_vars['selected'] = 1;
<{/php}>
And the smarty code:
<{html_options name=active id=active onchange=document.myform.submit(); options=$mymenu selected=$selected}>
The nice and most obvious reason to do this is so we don't have to keep redoing it over and over with PHP. Once the template has been created and cached it never gets done again.
So, now you know, you can use PHP in smarty just like others do in Joomla. Yes you can even include PHP files and work from them....
Have fun...
ATB
Catz