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

Variable callback

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
mwvdlee
Smarty Rookie


Joined: 05 Nov 2011
Posts: 7

PostPosted: Wed Jun 01, 2016 5:10 pm    Post subject: Variable callback Reply with quote

I'm using Smarty with multiple template files to generate user-defined pages. These template files may use any of a large number of variables (several hundred to over a thousand, depending on the complexity), which are relatively expensive to assign. Usually only a few dozen of those available variables are actually used.

I'd like to be able to have Smarty "pull" these variables from my PHP code by means of some callback function whenever a variable is undefined in `$_smarty_tpl->tpl_vars`, so only variables that are actually used are assigned (while still respecting any variables properly assigned). I could probably hack the `smarty_internal_data` class to do this, but is there any clean way of doing something like this. Perhaps using filters or such?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Jun 01, 2016 6:00 pm    Post subject: Reply with quote

It is not a very good idea to allow template access random data in your code.
It greatly diminishing the application security.
I suggest you reconsider your approach. Caching may help better, than shooting security in the head.
Back to top
View user's profile Send private message
mwvdlee
Smarty Rookie


Joined: 05 Nov 2011
Posts: 7

PostPosted: Wed Jun 01, 2016 6:14 pm    Post subject: Reply with quote

AnrDaemon wrote:
It is not a very good idea to allow template access random data in your code.
It greatly diminishing the application security.
I suggest you reconsider your approach. Caching may help better, than shooting security in the head.

Nobody's talking about accessing random data. I think you may have misunderstood what I'm asking. I'm talking about using a callback function that would take the requested variable name and return something. The callback function would have absolute control over what (if anything) it returns as a value. If you don't know what a callback function is, please take a look at PHP's standard `array_walk`, `array_map` and `preg_replace_callback`.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Jun 01, 2016 7:05 pm    Post subject: Reply with quote

No, I understand perfectly.
But you did not provide any numbers to back your claim of "expensive".
Back to top
View user's profile Send private message
mwvdlee
Smarty Rookie


Joined: 05 Nov 2011
Posts: 7

PostPosted: Wed Jun 01, 2016 7:17 pm    Post subject: Reply with quote

AnrDaemon wrote:
No, I understand perfectly.
But you did not provide any numbers to back your claim of "expensive".

That's because "expensive" is relative. In my current situation a typical "render" requires about 30k `assign()` calls (in itself rather expensive due to the construction of `Smarty_Variable` objects). Most of the cost is in the way these variable values are generated, sometimes having to go through several dozen methods due to the structure of the code. In total, the whole thing takes up roughly 30% to 40% of CPU time, as reported by Xdebug.

I assume you understand the concept of lazy evaluation, which is essentially what my question boils down to; only doing the work of processing and assigning the variables that are used, without knowing beforehand which ones are going to be used.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Jun 01, 2016 10:27 pm    Post subject: Reply with quote

Do note that xdebug itself is a big performance hog.
Instead of arguing to try and prove your case at any cost (while lokking more and more unconvincing at each iteration), try different approaches to your problem.
Use caching. It'll nullify the cost of variable assignement to the level of complete insignificance. (I once brought request average times from 3-5s to 0.1-0.2s through liberal use of cahing around only user-generated blocks.)
May be you don't need to assign all variables at all times.
May be you can assign these variables in bigger batches, resulting in fewer assinement calls.
And at all times test the implementation on real tasks. Not just look at one sample in XDebug and say "nono, it's expensive".
Back to top
View user's profile Send private message
mwvdlee
Smarty Rookie


Joined: 05 Nov 2011
Posts: 7

PostPosted: Thu Jun 02, 2016 5:44 am    Post subject: Reply with quote

AnrDaemon wrote:
Do note that xdebug itself is a big performance hog.
Instead of arguing to try and prove your case at any cost (while lokking more and more unconvincing at each iteration), try different approaches to your problem.
Use caching. It'll nullify the cost of variable assignement to the level of complete insignificance. (I once brought request average times from 3-5s to 0.1-0.2s through liberal use of cahing around only user-generated blocks.)
May be you don't need to assign all variables at all times.
May be you can assign these variables in bigger batches, resulting in fewer assinement calls.
And at all times test the implementation on real tasks. Not just look at one sample in XDebug and say "nono, it's expensive".


Regarding Xdebug; you think I'm sitting here with a stopwatch seeing how long it takes for Xdebug to run? Xdebug has a profiler, which is quite accurate.

Caching isn't an option, since the variables used can change at any time without my knowledge.

I indeed need not assign all variables at all times. The problem is that I cannot know which variables not to assign; that's the entire point.

Assigning variables in bigger batches is what I'm already doing; it still causes a lot of `Smarty_Variable` instantiation and does not avoid the cost of actually calculating the values of those variables.

I've run the profiler on real-world samples (and yes; multiple samples), why would you ever run a profiler on cases you know are invalid?

The answers provided above are for other people who may be reading this forum. It is clear that AnrDaemon is unwilling/unable to accept the question as given.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jun 02, 2016 1:15 pm    Post subject: Reply with quote

You just showed the level of ignorance I didn't expect from a compentent web developer.
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 -> Smarty Development 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