View previous topic :: View next topic |
Author |
Message |
tacman1123 Smarty Rookie
Joined: 11 Mar 2004 Posts: 8 Location: Washington, DC
|
Posted: Fri Apr 08, 2011 9:44 am Post subject: allowing dot(.) to access object properties OR arrays |
|
|
I often pass structured data into Smarty, sometimes as an array, sometimes as an object, depending on if it's coming from a cache, YAML, a MySQL database, a SQLite database, etc. Often I'm dependent on different libraries, which may return results in one arrays of arrays or arrays of objects.
Smarty throws an error if I confuse array access v. object access. I rarely care which type it is by the time I'm displaying it, but it's a pain to constantly cast the data into one type or the other, especially if the data is nested, and therefore is treated identically when using {foreach $object_or_array as $var=>$val}.
TWIG uses dot(.) for access to any array element or object property, and to call a method on an object (with parenthesis, obviously). I would LOVE for Smarty to do the same. The code would look cleaner, and anyone modifying the design, especially nested lists, wouldn't care about the underlying data representation. |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
|
Back to top |
|
salebab Smarty Rookie
Joined: 01 Dec 2010 Posts: 5
|
Posted: Thu Oct 13, 2011 9:55 pm Post subject: |
|
|
+1. It will be very nice if smarty developers implement this feature. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Oct 14, 2011 12:44 am Post subject: |
|
|
we have considered this long before. The biggest drawback is a bit of performance. Smarty would have to determine if the '.' represents array, object, etc. at runtime, and access as such. currently this is a compile time operation. there may be other concerns that are not coming to mind right now, will have to revisit. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Oct 14, 2011 1:40 am Post subject: |
|
|
Yes we would need runtime checks which will affekt performance.
It could get very ugly on contructs like $foo.bar.buh.blar which could be array access, object chaining or a combination of these.
The implementation if isset(), empty() and default modifer would become extremly difficult or not possible.
Looks like a no go for me. |
|
Back to top |
|
salebab Smarty Rookie
Joined: 01 Dec 2010 Posts: 5
|
Posted: Tue Oct 18, 2011 3:37 pm Post subject: |
|
|
Quote: | Smarty would have to determine if the '.' represents array, object, etc. at runtime, and access as such. currently this is a compile time operation |
You don't need to write code to determine that at runtime, every programmer who prefferer to work with objects and who is to lazy to write -> instead of . can add one line of code and "tells" to smarty how to compile variables. There is no need for auto determination.
Example:
Code: | $smarty->use_objects = true; // instead of array |
What you think? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Oct 18, 2011 3:40 pm Post subject: |
|
|
salebab wrote: | Quote: | Smarty would have to determine if the '.' represents array, object, etc. at runtime, and access as such. currently this is a compile time operation |
You don't need to write code to determine that at runtime, every programmer who prefferer to work with objects and who is to lazy to write -> instead of . can add one line of code and "tells" to smarty how to compile variables. There is no need for auto determination.
Example:
Code: | $smarty->use_objects = true; // instead of array |
What you think? |
what happens to arrays then? the dot is already used for arrays in the $smarty special var, we can't have either or. |
|
Back to top |
|
salebab Smarty Rookie
Joined: 01 Dec 2010 Posts: 5
|
Posted: Tue Oct 18, 2011 3:54 pm Post subject: |
|
|
Yes, but smarty is special var and can still be the same. Other arrays still can be used with regular brackets [], but personaly, I do not use arrays except array of object (collection).
These are just my thoughts... Btw, thx for reply... |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Oct 18, 2011 3:59 pm Post subject: |
|
|
salebab wrote: | Yes, but smarty is special var and can still be the same. Other arrays still can be used with regular brackets [], but personaly, I do not use arrays except array of object (collection).
These are just my thoughts... Btw, thx for reply... |
We still need to access arrays with dots. If you really want '.' to access objects except for $smarty. then make a simple prefilter that replaces all occurrences of these . with ->, or to whatever preference you want your templates to be. |
|
Back to top |
|
salebab Smarty Rookie
Joined: 01 Dec 2010 Posts: 5
|
Posted: Tue Oct 18, 2011 6:22 pm Post subject: |
|
|
You're probably right, didn't know for prefilter till now. Thanks. |
|
Back to top |
|
imbafuzz Smarty n00b
Joined: 11 Nov 2011 Posts: 1
|
Posted: Fri Nov 11, 2011 4:32 pm Post subject: |
|
|
Thanks nice information, looking forward to this thread...
Regards,
windows tips |
|
Back to top |
|
|