Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Load plugin from PHP

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
ImNotAGeekReally
Smarty Rookie


Joined: 27 Oct 2009
Posts: 5

PostPosted: Tue Oct 27, 2009 12:10 pm    Post subject: Load plugin from PHP Reply with quote

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
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 259

PostPosted: Tue Oct 27, 2009 1:11 pm    Post subject: Re: Load plugin from PHP Reply with quote

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 Wink ...
Back to top
View user's profile Send private message
ImNotAGeekReally
Smarty Rookie


Joined: 27 Oct 2009
Posts: 5

PostPosted: Tue Oct 27, 2009 1:14 pm    Post subject: Reply with quote

$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 Smile

It physically passes the function name through to Smarty, just it won't parse as a function
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 419
Location: Kathmandu, Nepal

PostPosted: Tue Oct 27, 2009 6:35 pm    Post subject: This is possible Reply with quote

It looks like, it is a creative problem Wink with some solutions possible.

Quote:
{$plugin}

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
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
ImNotAGeekReally
Smarty Rookie


Joined: 27 Oct 2009
Posts: 5

PostPosted: Tue Oct 27, 2009 10:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 419
Location: Kathmandu, Nepal

PostPosted: Wed Oct 28, 2009 6:56 am    Post subject: Running Examples Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
ImNotAGeekReally
Smarty Rookie


Joined: 27 Oct 2009
Posts: 5

PostPosted: Wed Oct 28, 2009 9:03 am    Post subject: Reply with quote

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
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 419
Location: Kathmandu, Nepal

PostPosted: Wed Oct 28, 2009 9:56 am    Post subject: no dynamic names. Reply with quote

You can call smarty_function_festival() within your box loader. Or adjust to rewrite it a little.

Just, we can not set the {$box_name} as a dynamic smarty function.
_________________
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
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP