1
Catzwolf
Xoops Objects directly from Smarty...... Way Cool!!!
  • 2010/7/24 4:49

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


For the last few months I have been playing around with different ideas I have been having regarding getting more for my code using less.

The best way is to make use of smarty more and more, so the less code I have, in reality it is better for me. This means there is less to go wrong and easier to maintain.

The normal way of assigning variables is through smarty and normally this would be done in this manner(or something like it)

$my_handler = &xoops_getmodulehandler'myhandler''moduledir' );
$myObjectsArray $my_handler->getObjects();
foreach ( 
$myObjectsArray as $myObject ){
    
$newArray[] = array(
        
'id' => $myObject->getVar'id' ),
        
'title' => $myObject->getVar'title' ),
        
'image' => $myObject->getImage(),
        
'published' => $myObject->getPublished()
        );
}
$xoopsTpl->assign_by_ref'newarray'$newArray );


This is the way most of us do it, but there is an easier way and one that will make it easier in the long run.

$my_handler = &xoops_getmodulehandler'myhandler''moduledir' );
$myObjectsArray $my_handler->getObjects();
$xoopsTpl->assign_by_ref('myObjectsArray'$myObjectsArray );


And now you can access the Object straight from Smarty, using a line such as:

<{foreachq item=obj from=$myObjectsArray}>
    <
div><{$obj->getVar('id')}>div>
    <
div><{$obj->getVar('title')}>div>
    <
div><{$obj->getVar('image')}>div>
    <
div><{$obj->getVar('published')}>div>
<{/foreach}>


Or like this:

$my_handler = &xoops_getmodulehandler'myhandler''moduledir' );
$myObject $my_handler->get(1);
$xoopsTpl->assign_by_ref'myObject'$myObject );


<{$myObject->getVar('post_text')}>



I hope others find this useful and helpful :)

ATB

Catz

2
trabis
Re: Xoops Objects directly from Smarty...... Way Cool!!!
  • 2010/7/24 12:14

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


I had no idea we could use objects in smarty, this is very good to know!

Anyway, I fear that templates should deal with presentation logic and the use of objects may defeat that purpose if misused.

Thank you!

3
Catzwolf
Re: Xoops Objects directly from Smarty...... Way Cool!!!
  • 2010/7/24 16:08

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


For some reason last night when I was posting this, I knew you would be the one to respond to this, thanks :)

I had an idea that this could be possibly done but it was only really last night I put my mind to it and came up with a working solution.

The whole point is even with smarty we are using arrays and objects all the time, even though they appear as smarty tags, in reality they are still what they are.

I see no real difference really....

$category_array[] = array(
    
'cat_id' => $catObject->getVar'cat_id' ),
    
'cat_title' => $catObject->getVar'cat_title' ),
    
'cat_image' => $catObject->getImage(),
    
'cat_sponsor' => $catObject->getSponser(),
    
'cat_description' => $catObject->getVar'cat_description' ),
    
'forums' => $catObject->getForums$forumsByCat )
    );
$xoopsTpl->assign_by_ref'categories'$category_array );


So to access we would do

<{foreachq item=category from=$categories}>
  <{
$category.cat_title}>
<{/foreach}>

<{
foreachq item=category from=$categories}>
  <{
$category->getVar('cat_title')}>
<{/foreach}>


Basically, all we are doing is taking an array of object and turning them into an array of arrays, which means adding more time to the execution of the script.

We the developers still have full control over the logic and the designers would still treat those as logic and in very much the same way they would now.

But yes there could be a temptation to do our logic right here in the template, but there will always be some logic involved no matter what with smarty and this just 'cuts' out the middle man I think.

Plus, really all our logic should be done within the handlers and not within the controller part of our modules. Which it seems a lot of us developers still do.



4
luciorota
Re: Xoops Objects directly from Smarty...... Way Cool!!!
  • 2010/7/24 17:42

  • luciorota

  • Module Developer

  • Posts: 216

  • Since: 2007/4/20


I't a great possibility.. really...
Thanks Catzwolf, your "playing" is...

U R a Wizard!!!
Rota Lucio
lucio.rota@gmail.com;
mobile: +39 338 9966321

Login

Who's Online

352 user(s) are online (198 user(s) are browsing Support Forums)


Members: 0


Guests: 352


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits