SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Perth, Australia
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Objects versus arrays in templates

    Hi,
    I've been toying with the idea of using objects with the smarty templating engine and wanted to find out a few opinions

    I've been using arrays in the past, eg:
    PHP Code:
    $some_array = array(
           array(
    'id' => 1'name' => 'person1'),
           array(
    'id' => 2'name' => 'person2'),
           array(
    'id' => 3'name' => 'person3'
    );
    $smarty->assign('some_array',$some_array); 
    HTML Code:
    {section name=c loop=$some_array}
        {$some_array[c].id} - {$some_array[c].name}<br />
    {/section}
    but, if you assume the 2D array is now an array of objects with the same property names:
    HTML Code:
    {section name=c loop=$some_array}
        {$some_array[c]->getID()} - {$some_array[c]->getName()}<br />
    {/section}
    Both obviously work, I just wanted to know if there are any pros/cons to using objects or if I should just stick with using arrays?

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2004
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    savant .. again where is my bad *** programmer avatar

    I built something similar to savant several years ago. I go the object route with php as my template engine.

    a 3rd comment on the page is a good read for you.
    http://phpsavant.com/yawiki/

    objects with a good compiler can give you compile time errors. arrays can only yield runtime errors.

    I would consider this question similar to whats the benefit of struct and objects.
    answer: private variables use in member functions.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Perth, Australia
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks leblanc, I see your point with the compile time vs runtime errors.

    I know there's this big debate on template engines, and whats right and whats not, I've just got comfortable using smarty, but might try savant now that I know about it.

    Also, in case you're interested, using arrays was faster, but hey... whats a few milliseconds these days?!

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2004
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    i don't endorse savant

    I just want to be clear that I've never used savant. It was posted here a long time ago... and noticed I was using a similar approach of using objects inside templates.

    --- looks like they started adding more ideas along the line of smarty

    concerning speed:
    there is no comparison to objects which give you access to abilities not just raw data that would require calculations in your views.


    if your interested in my version of savant..
    very old posts of mine:
    http://www.sitepoint.com/forums/showthread.php?t=438893
    http://www.sitepoint.com/forums/showthread.php?t=398697

    I'll probably revisit this in the next couple of months.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A few unsolicited comments:

    Be careful about the types of objects you pass in to your template. I don't recommend passing in anything that can alter the data model (say via set() or through side-effects of methods) nor do I think it is wise to start using get() patterns on your passed in objects. If you need the latter, then I'd say it is better to use overloading since Smarty doesn't support dereferencing methods.

    Finally, there is typically a temptation to pass in your application objects wholesale and unadulterated. I think this is a bad practice that can kill your separation of concerns. Further, doing so typically injects new behaviors into the template via the data model rather than via the usual plugin system.

    In my mind, it is better to mold custom objects that specifically represent the template data interface -- if you use objects at all.

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > Finally, there is typically a temptation to pass in your application objects wholesale and
    > unadulterated.

    I would agree with that; Your object hierarchy should be guarded using an Interface at the very minimum. Further to that, the object hierarchy would be better off having little, or no behaviour at all, so keep them dumb...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •