View previous topic :: View next topic |
Author |
Message |
Xof Smarty n00b
Joined: 13 Aug 2008 Posts: 4
|
Posted: Wed Aug 13, 2008 4:44 am Post subject: The big difference between register_object and assign_by_ref |
|
|
After an exciting time debugging a template, I discovered what is probably the most critical difference between register_object and assign_by_ref:
1. register_object is processed at compile time;
2. assign_by_ref is processed at template render time.
The background: My templates include a {load} tag which is the point at which the database is hit. The theory is that, if the template is cached, there's no need to hit the database. {load} creates the objects required for the rest of the page.
This doesn't work at all with register_object. Since the objects do not exist (and thus are not called) until {load} is actually executed, the various {object->method} calls fail to compile.
assign_by_ref works just fine in this case. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Aug 13, 2008 3:32 pm Post subject: |
|
|
This is not really true. Registered objects and objects assigned by ref are both processed at template rendering time.
Assign_by_ref and register_object both require that the objects to exist when these methodes are being called. Did you do both in your {load} plugin?
Caching could not have been the problem, because the final html output is being cached.
Are you aware that parameter passing is different with registered objects and objects assigned by ref? |
|
Back to top |
|
Xof Smarty n00b
Joined: 13 Aug 2008 Posts: 4
|
Posted: Wed Aug 13, 2008 6:49 pm Post subject: |
|
|
U.Tews wrote: | This is not really true. Registered objects and objects assigned by ref are both processed at template rendering time. |
This is true. (Although see below on "processed.")
Quote: | Assign_by_ref and register_object both require that the objects to exist when these methodes are being called. |
This is true.
Quote: | Did you do both in your {load} plugin? |
Yes. (Meaning that I tried both, not that I did both at the same time, of course!)
I believe we mean slightly different things by "processed," though.
It appears that, if using register_object, a template tag of the form:
{object->method}
will given a compilation error if the register_object had not been done before template compilation.
However, using assign_by_ref, a template tag of the form:
{$object->method()}
will compile successfully even if assign_by_ref('object', $object) had not yet been called at compilation time. This allows me to put the assign_by_ref in to the 'load' method discussed above. Of course, if I screw up and forget to call assign_by_ref before that tag is expanded (as opposed to compiled), I assume I'll get an error then.
Does that seem right? It matches my experience, at least. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Aug 13, 2008 7:00 pm Post subject: |
|
|
Oh yes, you are rigth. Smarty tries to figure out what type of tag {object->method} is. As it is not yet registered at compile time it fails.
If you use assign by ref its clear because the syntax of the variable tags that it must be a variable ( because of {$....}).
This problem exist wether you are using caching or not. |
|
Back to top |
|
Xof Smarty n00b
Joined: 13 Aug 2008 Posts: 4
|
Posted: Wed Aug 13, 2008 7:11 pm Post subject: |
|
|
U.Tews wrote: | This problem exist wether you are using caching or not. |
Right! The idea was to avoid having to execute the {load} method at all, and the way I was going about that was to rely on the caching.
It occurs to me that another way of handling this is to pre-create all of the objects that the template might use, call register_object on them, but not actually populate them with data until the actual method call (basic lazy evaluation). That might be an even better approach, and it avoids having to have a magic {load} method. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Aug 13, 2008 7:46 pm Post subject: |
|
|
Or do all that work if the page is not in already in the cache.
Code: |
if (!$smarty->is_cached('template.tpl')) {
// create and register objects
// load database data and assign them to smarty
}
$smarty->display('template.tpl');
|
|
|
Back to top |
|
Xof Smarty n00b
Joined: 13 Aug 2008 Posts: 4
|
Posted: Wed Aug 13, 2008 8:32 pm Post subject: |
|
|
That's a great idea. Sadly, many (most?) of my templates have uncached sections in them, so I can't be sure that they don't need processing anyway. |
|
Back to top |
|
|