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

allowing dot(.) to access object properties OR arrays

 
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  
Author Message
tacman1123
Smarty Rookie


Joined: 11 Mar 2004
Posts: 8
Location: Washington, DC

PostPosted: Fri Apr 08, 2011 9:44 am    Post subject: allowing dot(.) to access object properties OR arrays Reply with quote

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
View user's profile Send private message AIM Address
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Apr 08, 2011 11:17 am    Post subject: Re: allowing dot(.) to access object properties OR arrays Reply with quote

http://www.smarty.net/forums/viewtopic.php?p=57177#57177
Back to top
View user's profile Send private message
salebab
Smarty Rookie


Joined: 01 Dec 2010
Posts: 5

PostPosted: Thu Oct 13, 2011 9:55 pm    Post subject: Reply with quote

+1. It will be very nice if smarty developers implement this feature.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Oct 14, 2011 12:44 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Oct 14, 2011 1:40 am    Post subject: Reply with quote

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
View user's profile Send private message
salebab
Smarty Rookie


Joined: 01 Dec 2010
Posts: 5

PostPosted: Tue Oct 18, 2011 3:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Oct 18, 2011 3:40 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
salebab
Smarty Rookie


Joined: 01 Dec 2010
Posts: 5

PostPosted: Tue Oct 18, 2011 3:54 pm    Post subject: Reply with quote

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... Smile
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Oct 18, 2011 3:59 pm    Post subject: Reply with quote

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... Smile


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
View user's profile Send private message Visit poster's website
salebab
Smarty Rookie


Joined: 01 Dec 2010
Posts: 5

PostPosted: Tue Oct 18, 2011 6:22 pm    Post subject: Reply with quote

You're probably right, didn't know for prefilter till now. Thanks.
Back to top
View user's profile Send private message
imbafuzz
Smarty n00b


Joined: 11 Nov 2011
Posts: 1

PostPosted: Fri Nov 11, 2011 4:32 pm    Post subject: Reply with quote

Thanks nice information, looking forward to this thread...
Regards,
windows tips
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