Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Plugins Custom Directory

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


Joined: 22 Jan 2017
Posts: 4
Location: United Kingdom

PostPosted: Sun Jan 22, 2017 12:29 pm    Post subject: Plugins Custom Directory Reply with quote

Hi

Smarty Version 2.6.18
LAMP on Lunbuntu

I am trying to customise an existing website which uses Smarty Templates. I have successfully written some plugins which reside in the default plugins directory. However I wish to separate my custom plugins from the site's orginal plugins (in case of upgrades etc) but I am struggling to understand how to add a custom plugins directory.

I have read up a fair bit on this but not having much luck. I have a number of questions:-

1. Why do any of the methods such as
$smarty->getPluginsDir
$smarty->setPluginsDir
fail with errors such as "Call to member function getPluginsDir() on a non-object"?

2. Where do I put the code to add to the plugins_dir array, anyway? Should such calls reside in the .tpl files? Do they need to be in PHP tags? Or should they be in the PHP files which call the .tpl files. Do they need to be in each .tpl file or can I configure it 'globally'

3. I have created my own plugins directory well away from the 'core' directories - do I have to specify the full path to my directory when I add it to the plugins_dir array? Should I use relative paths or specific paths to it when i do that? I ask because although I can see (literally) just 'plugins' in my plugins_dir array there does not seem to be a parent hierarchy to that folder anywhere? To clarify I was expecting to see the full directory path (e.g. "/var/www/html/my_site/global/smarty/plugins") in the default plugins_dir not just 'plugins'?

Thanks for any guidance.
Back to top
View user's profile Send private message
bsmither
Smarty Regular


Joined: 20 Dec 2011
Posts: 73
Location: Rocky Mountains

PostPosted: Sun Jan 22, 2017 8:22 pm    Post subject: Reply with quote

You mention using Smarty 2, so we need to limit our implementation to that codebase.

Which means, the methods described in #1 are inappropriate to use.

I interpret the docs as saying the directory to add to the plugins directory array can be in several places. You mention you want it away from anything that won't let it survive an upgrade, so we need to understand what kind of upgrade will happen.

"Nuke and Pave?" Then we need to put it outside the application's root folder. "Whole Cloth"? then we can put it almost anywhere.

But for the sake of simplicity, let's use an absolute path. So, find the absolute path to your application. It might be, for example:
/home/websites/user_grey/html_root/

Make a sibling directory:
/home/websites/user_grey/html_accessories/

In there, make a subdirectory:
/home/websites/user_grey/html_accessories/my_smarty_plugins/

That absolute path is what you would have as the argument to:
Code:
$smarty->plugins_dir[] = '/home/websites/user_grey/html_accessories/my_smarty_plugins';

This is a PHP statement, so it needs to be in a script that gets executed in the same execution session as the application, helpfully where Smarty gets instantiated.

Smarty looks in several locations automatically for the named plugin - and it might need to be "registered". You may have that figured out already.
Back to top
View user's profile Send private message
smarty_gray
Smarty n00b


Joined: 22 Jan 2017
Posts: 4
Location: United Kingdom

PostPosted: Sun Jan 22, 2017 8:57 pm    Post subject: Plugins Custom Directory Reply with quote

Hi bsmither

Thanks for the very much for the suggestions which I have experimented with; I still seem to be having trouble however.

I created a simple plugin (cit_test_js) which I placed in the default plugins directory. I then tested it to make sure it worked. I then moved the plugin its new home (as below)

In the .tpl file I placed the following code (long-handed hierarchy for now)
Code:

{php}
$smarty->plugins_dir[] = '/var/www/html/my_site/cit_modules/global/smarty/plugins';
{/php}
{cit_test_js}


But the page failed to render. I then commented out cit_test_js, the page loaded OK.

I added {$smarty->plugins_dir[]|@debug_print_var} to see if I could confirm if the addition had worked but again the page failed to render?

What's the best way of listing the entries in the plugins_dir variable please?
Back to top
View user's profile Send private message
smarty_gray
Smarty n00b


Joined: 22 Jan 2017
Posts: 4
Location: United Kingdom

PostPosted: Sun Jan 22, 2017 9:08 pm    Post subject: Plugins Custom Directory Reply with quote

Hi

Ahh... making some progress. I amended my code to read :-

Code:
{php}
$this->plugins_dir[] = '/var/www/html/my_site/cit_modules/global/smarty/plugins';
var_dump($this->plugins_dir[]);
{/php}

And the array has been updated.

Unfortunately, when I call my plugin as per {cit_test_js}, the page fails to render again?

I think I read something about a smarty console? Would it be able to help?

Thanks
Back to top
View user's profile Send private message
bsmither
Smarty Regular


Joined: 20 Dec 2011
Posts: 73
Location: Rocky Mountains

PostPosted: Mon Jan 23, 2017 6:06 pm    Post subject: Reply with quote

Allow me to mention again:
"This is a PHP statement, so it needs to be in a PHP script that gets executed in the same execution session as the application, helpfully where Smarty gets instantiated."

So, let's find in the PHP application code that location where one would find the existing instantiation of Smarty:
Code:
FIND:
$smarty = new Smarty();

After that, you would add:
Code:
$smarty->plugins_dir[] = '/var/www/html/my_site/cit_modules/global/smarty/plugins';

I will urge you to stop believing this statement can go in a template. It goes in your application code.
Back to top
View user's profile Send private message
smarty_gray
Smarty n00b


Joined: 22 Jan 2017
Posts: 4
Location: United Kingdom

PostPosted: Mon Jan 23, 2017 6:34 pm    Post subject: Plugins Custom Directory - Solved! Reply with quote

Hi

Thanks again for the nudge. I have now found exactly the place in this application where PHP is used to call the templates. I have added my custom directory in there (as per your instructions) and, voila, my test plugin runs!

It did need another slight alteration in that this particular application writes $smarty to another global variable and it was that, alternate, global variable I needed to amend.

Thanks very much for the pointers!
Back to top
View user's profile Send private message
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