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

access an object's multi dimensional array attribute

 
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 -> General
View previous topic :: View next topic  
Author Message
falke
Smarty n00b


Joined: 01 Aug 2003
Posts: 2

PostPosted: Fri Aug 01, 2003 9:58 pm    Post subject: access an object's multi dimensional array attribute Reply with quote

I have created a class for search hits in a database.
The class has a

hits('field1'=>array(field1value1,field1value2,,field1value3,...,field1valueN) 'field2'=>array(field2value1,field2value2,,field2value3,...,field2valueN),
.'fieldk'=>array(fieldkvalue1,fieldkvalue2,,fieldkvalue3,...,fieldkvalueN))

data member.
I would like to access the components of this data member through template using register_object.
How can I do this ?
Back to top
View user's profile Send private message
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Thu Aug 07, 2003 7:20 pm    Post subject: Reply with quote

Hi,
strange dump output Shocked
The example in the manual is a bit buggy. here's a working one based on the manual but using your "hits" property as well.
Note that I simply re-used the same object ($myobj), but registered it under different names: 'foobar1', 'foobar2', 'foobar3' using any of the methods, and as a normal variable '$foobar4'.
$hits['field3'] is again another associative array (hash), to illustrate it's usage ([color:blue]the blue colored part[/color])

[php:1:15506638fa]<?php
// the object
class My_Object {
var $hits = array();
function My_Object() {
$this->hits = array(
'field1' => array('value11', 'value12', 111),
'field2' => array('value21', 'value22', 222),
'field3' => array('key31'=>'value31', 'key32'=>'value32')
);
}
function meth1($params, &$smarty_obj) {
$smarty_obj->assign('NewVal', $params['p1']);
echo "<br>- direct output by meth1: keys = " . implode(',', array_keys($params)); # will be echoed to the page
return "<br>- this is meth1. params = " . implode(',', array_values($params));
}
function meth2($params, &$smarty_obj) {
return "<br>- this is meth2. params = " . implode(',', array_values($params));
}
function meth3($params, &$smarty_obj) {
return "<br>- this is meth3. keys = " . $params;
}

}

$myobj = new My_Object;
// registering the object (will be by reference)
$smarty->register_object("foobar1", $myobj);
// if we want to restrict access to certain methods or properties, list them
$smarty->register_object("foobar2", $myobj, array('meth2','hits'));
// if you want to use the traditional object parameter format, pass a boolean of false
$smarty->register_object("foobar3", $myobj, null, false);

// We can also assign objects. Assign by ref when possible.
$smarty->assign_by_ref("foobar4", $myobj);

$smarty->display("objects.tpl");
?>
[/php:1:15506638fa]
Quote:
{* access our registered object *}
<p>Object foobar1->meth1: {foobar1->meth1 p1="foo1" p2=$bar}
<br>- assigned var '$NewVal' by in meth1: {$NewVal}</p>

{* you can also assign the output *}
<p>Object foobar2->meth2: {foobar2->meth2 p1="foo2" p2=$bar assign="output2"}
<br>- the output2 was {$output2}</p>

[color=blue<p>Objectproperty foobar2->hits: {foobar2->hits assign="hits2"}
<br>- field1 by numerical index are "{$hits2.field1[0]}" and "{$hits2.field2[0]}"</p>
<p>Objectproperty foobar2->hits: {foobar2->hits assign="hits3"}
<br>- field3 by named index are "{$hits3.field3.key31}" and "{$hits3.field3.key32}"</p>
[/color]

<p>Object foobar3->meth3: {foobar3->meth3 gaga="foo3" blub=$this assign="output3"}
<br>- the output3 was {$output3}</p>

{* access our assigned object variable *}
<p>Variable $foobar4->meth3: {$foobar4->meth3("foo4",$bar)}</p>


And a final note to the documentation team: register_object() is (still) missing from the german docs! I had to downloaded the english manual to find out, what this is about...

Have fun,
CirTap
Back to top
View user's profile Send private message
falke
Smarty n00b


Joined: 01 Aug 2003
Posts: 2

PostPosted: Fri Aug 08, 2003 7:10 pm    Post subject: Reply with quote

Thank you for your kindly help.
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 -> General 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