Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Auto registering functionsas cacheable

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  

Do you like this idea?
Yes
100%
 100%  [ 3 ]
No
0%
 0%  [ 0 ]
Total Votes : 3

Author Message
Ztyx
Smarty n00b


Joined: 21 Jan 2007
Posts: 2

PostPosted: Sun Jan 21, 2007 5:51 pm    Post subject: Auto registering functionsas cacheable Reply with quote

Hi,
I like the feature of being able to put my own plugins in directory and then leaning back as smarty does the job of including them as needed.

Until...I realised that I had to manually register (using 'register_*()' functions) all of the functions that I wanted to be non-cacheable.

A very nice feature would be to have for example:
Code:
$smarty = new Smarty();
$smarty->plugins[] = 'cacheable-plugins-path';
$smarty->nocache_plugins[] = 'non-cacheable-plugins-path'; /* non-cacheable plugins */


Another way of implementing this would be to have an optional special global variable in all the plugin-files which could be set when Smarty includes the file. Example:
Code:
/* disables caching of all functions in this file */
$smarty_nocache = true;

function smarty_function_foo($params, &$smarty)
{
  ...
}


We will see if I am in a good mood to implement this myself...

Jens
Back to top
View user's profile Send private message
wid
Smarty n00b


Joined: 30 Jan 2007
Posts: 2

PostPosted: Tue Jan 30, 2007 6:09 pm    Post subject: Nice, what about runtime caching choice ? Reply with quote

Hi,

I need this feature also !

But I wonder if it might be better if we could decide to cache or not at runtime instead ?

Something like:
Code:

$smarty->current_plugin->caching = true;
Back to top
View user's profile Send private message
wid
Smarty n00b


Joined: 30 Jan 2007
Posts: 2

PostPosted: Wed Jan 31, 2007 2:08 pm    Post subject: Implemented the second proposition of Ztyx Reply with quote

Hi,

This code enable an hook on a function in the plugin file that return the desired caching strategy.
The function is only called only called once, the first time the plugin is found.
It is not called at every plugin call.
Code:

+++ Smarty-2.6.16/libs/Smarty_Compiler.class.php        (working copy)
@@ -787,8 +787,8 @@
                 $message = "plugin function $plugin_func() not found in $plugin_file\n";
                 $have_function = false;
             } else {
-                $this->_plugins['function'][$tag_command] = array($plugin_func, null, null, null, true);
-
+               $cacheable = function_exists("{$plugin_func}_cache") ? call_user_func("{$plugin_func}_cache", $this) : true;
+               $this->_plugins['function'][$tag_command] = array($plugin_func, null, null, null, $cacheable);
             }
         }


And the function in the plugin file "function.text.php" looks like that:
Code:

function smarty_function_text($params, &$smarty) {
}

function smarty_function_text_cache($smarty) {
  return false;
}


It only works for "function" blocks.
The function should also define cache_attrs and work at each inclusion but this need a bigger work.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jan 31, 2007 6:24 pm    Post subject: Reply with quote

Hi.

I've pondered this many times. The Smarty 2.x implementation of plugins is not very amenable to extensions that might support this and I don't think ad-hoc provisions are a good idea. That said, the two directory approach is not so bad.

Still, I think this is better suited for configuration. The problem here, of course, is that if you manually register, you must also manually load plugins. So I was wondering -- and bear in-mind that I am only thinking out loud here -- what if, when registering, we allowed the second parameter (the 'implementation') to be null (or some suitable constant)? In that case, Smarty should be made to understand that the implementation should still be autoloaded if indeed it is called for. Off the top of my head, this can probably be done entirely in smarty_core_load_plugins().

Maybe a bad idea. I'd rather see this thought out more fully, FWIW.
Back to top
View user's profile Send private message
cydo
Smarty n00b


Joined: 25 Mar 2007
Posts: 2

PostPosted: Sun Mar 25, 2007 4:52 pm    Post subject: Reply with quote

Hello,

a) plugins directory:
we should have an additional user-plugin directory located in the same directory tree as the templates (or elsewhere, but not in the libs/ directory)

/mydomain/htdocs/index.php:
Code:
require '/mydomain/libs/smarty/libs/Smarty.class.php';
...
$smarty->template_dir = '/mydomain/smarty/templates';
$smarty->userplugins_dir = '/mydomain/smarty/plugins'

to seperate (read-only) "system plugins" (/mydomain/libs/smarty/libs/plugins/) from user plugins and helps identifying user written code whilst updating smarty.

b) auto-cachable Plugins:
Well, it should be done in the template itself:

/mydomain/smarty/templates/index.tpl:
Code:
should we do it: {eightball param1=random param2=whatever #caching=false}


with # as a delimiter to the parameters, so it's not passed to the function "eightball" itself.

To set a function globally cacheable without using #caching=true ("true" could be the default value to maintain older smarty behavior) in every template function call I like wid's idea:

Code:
$smarty->eightball->caching = true;
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Mar 25, 2007 11:13 pm    Post subject: Reply with quote

@cydo: You can already separate your plugin directories. Just assign plugins_dir as an array of directory paths. The paths are searched in the order given in the array.
Back to top
View user's profile Send private message
cydo
Smarty n00b


Joined: 25 Mar 2007
Posts: 2

PostPosted: Mon Mar 26, 2007 7:14 pm    Post subject: Reply with quote

Quote:
You can already separate your plugin directories. Just assign plugins_dir as an array of directory paths. The paths are searched in the order given in the array.

oh. didn't noticed that... well, time to clean up my plugin directory Wink thnx
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests 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