| View previous topic :: View next topic |
| Author |
Message |
ImNotAGeekReally Smarty Rookie
Joined: 27 Oct 2009 Posts: 5
|
Posted: Tue Oct 27, 2009 12:10 pm Post subject: Load plugin from PHP |
|
|
Greetings Fellow Smarty-ers
I'm trying to create an ecom site with a 3 column layout. On the two sidebars, I'm writing all the boxes as Smarty Plugins which work very nicely.
What I want to do is load the functions into a PHP array, pass that through to Smarty and then get Smarty to call the functions. However, I'm having difficulty as all I'm getting is the function name displayed.
For example....
PHP Code
=================
$arrSidebox = array(
'festivals',
'newsletter'
)
$objSmarty->assign('sidebox', $arrSidebox);
Smarty Code
=================
<div id="columnRight">
{foreach from=$sidebar item=plugin}
{$plugin}
{/foreach}
</div>
I've tried various different ways of getting {pluginName} to be parsed through to Smarty but I can get it working.
Anyone got any ideas?
Thanks
Simon |
|
| Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 259
|
Posted: Tue Oct 27, 2009 1:11 pm Post subject: Re: Load plugin from PHP |
|
|
| ImNotAGeekReally wrote: |
PHP Code
=================
$arrSidebox = array(
'festivals',
'newsletter'
)
$objSmarty->assign('sidebox', $arrSidebox);
|
Have you tried this?
| Code: | | $Smarty->assign('sidebox', $arrSidebox); |
http://www.smarty.net/manual/en/api.assign.php
| ImNotAGeekReally wrote: |
Smarty Code
=================
<div id="columnRight">
{foreach from=$sidebar item=plugin}
{$plugin}
{/foreach}
</div>
|
The value in PHP is named sidebox and in Smarty sidebar ... |
|
| Back to top |
|
ImNotAGeekReally Smarty Rookie
Joined: 27 Oct 2009 Posts: 5
|
Posted: Tue Oct 27, 2009 1:14 pm Post subject: |
|
|
$objSmarty is the same as $smarty. It's just the name of my PHP object
I use Hungarian naming conventions, else I get beat up
It physically passes the function name through to Smarty, just it won't parse as a function |
|
| Back to top |
|
bimal Smarty Elite

Joined: 19 Apr 2007 Posts: 419 Location: Kathmandu, Nepal
|
Posted: Tue Oct 27, 2009 6:35 pm Post subject: This is possible |
|
|
It looks like, it is a creative problem with some solutions possible.
Please note that {$plugin} won't be a dynamic plugin name to run. Rather, it will print the values of what $plugin has. For example, in the first loop, it will be festivals.
But, if you wanted festivals as a box, rather than anything else, you can tweak your coding a bit. Let us write another plugin named loadBox modifier. And, within the loop, you will write:
| Code: | <div id="columnRight">
{foreach from=$sidebar item=plugin}
{$plugin|loadBox}
{/foreach}
</div> |
Have you noticed: {$plugin|loadBox} in this code?
Save a file named: modifier.loadBox.php into your plugins folder and write a function there as:
| Code: | function smarty_modifier_loadBox($box_name='')
{
# Here, you will read your box file
# You can even use: global $smarty; and access the methods in order to manipuate the box name into box contents.
....
# and finally return it.
return($box_contents);
} |
This is a concept only, and should work for you. _________________ Skype: pbimal
To hire instantly as a freelancer - https://www.odesk.com/o/profiles/users/_~~657b70cc7f2c616a/
Visit my website - http://bimal.org.np/ for more articles
800+ screenshots to learn about website mistakes: http://mistakes.sanjaal.com/ |
|
| Back to top |
|
ImNotAGeekReally Smarty Rookie
Joined: 27 Oct 2009 Posts: 5
|
Posted: Tue Oct 27, 2009 10:27 pm Post subject: |
|
|
I see where you're going with that and I like the idea.
Afraid I need a little more help on this though.
I've got the loadBox modifier created, but I don't entirely know what to put in there to output the custom function.
So far I've got.......
| Code: |
function smarty_modifier_loadBox($box_name='') {
global $objSmarty;
}
|
I'm fairly convinced that its a case of something like
| Code: | | $objSmarty->doSomethingMagicalToCallTheFunction |
But I don't know what that function would be. |
|
| Back to top |
|
bimal Smarty Elite

Joined: 19 Apr 2007 Posts: 419 Location: Kathmandu, Nepal
|
Posted: Wed Oct 28, 2009 6:56 am Post subject: Running Examples |
|
|
I have created a running example for you now.
In your PHP:
| Code: | $sidebox = array(
'festivals',
'newsletter'
);
$smarty->assign('sidebox', $sidebox);
$smarty->display('boxes-test.tpl'); |
<Smarty Templates>/boxes-test.tpl
| Code: | <div id="columnRight">
{foreach from=$sidebox item='box'}
<h3 class="box-name">{$box}</h3>
<div class="box-contents">
{$box|loadBox}
</div>
{/foreach}
</div> |
<Smarty Plugins>/modifier.loadBox.php
| Code: | <?php
/**
* Load a sitebar box
*/
function smarty_modifier_loadBox($boxName='')
{
global $smarty;
$box_template = "boxes/{$boxName}.tpl";
if($smarty->template_exists($box_template))
{
return($smarty->fetch($box_template));
}
else
{
return("<!-- Box Error: {$box_template} does not Exist. -->");
}
} # loadBox()
?> |
<Smarty Templates>/boxes/festivals.tpl
| Code: | <p>Chrismas</p>
<p>Gir's Day</p>
<p>Valentine</p> |
<Smarty Templates>/boxes/newsletter.tpl
| Code: | <p><a href="afr.php">Annual Financial Reports</a></p>
<p><a href="on.php">Other Newsletters</a></p> |
Hopefully, this helps you a lot. _________________ Skype: pbimal
To hire instantly as a freelancer - https://www.odesk.com/o/profiles/users/_~~657b70cc7f2c616a/
Visit my website - http://bimal.org.np/ for more articles
800+ screenshots to learn about website mistakes: http://mistakes.sanjaal.com/ |
|
| Back to top |
|
ImNotAGeekReally Smarty Rookie
Joined: 27 Oct 2009 Posts: 5
|
Posted: Wed Oct 28, 2009 9:03 am Post subject: |
|
|
Thanks for that. Because the festivals and newsletterSignup functions have quite a substantial amount of PHP in, I actually want to include the smarty_function_festival() function, rather than just the template.
Is there any way you can think of amending the loadBox modifier so it calls the function? |
|
| Back to top |
|
bimal Smarty Elite

Joined: 19 Apr 2007 Posts: 419 Location: Kathmandu, Nepal
|
|
| Back to top |
|
|