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