Page 1 of 1

[SOLVED] Get and display variables from objects / arrays with smarty

Posted: Thu Apr 08, 2010 12:09 am
by jomoweb
I am having a tough time trying to extract variables from an array within an object. I am hoping that someone can help me figure out my issue and give me a quick run-down on how this to properly output variable values.

After doing a print_r dump of the Products object (I am working with the Products Module), I find the variable I am looking for and see the correct value.

In a node in $Products->cms->siteprefs, I find: [Products_mapi_pref_autopreviewimg] => 0

Code: Select all

{$Products->cms->siteprefs->Products_mapi_pref_autopreviewimg}
Outputs: Nothing

Code: Select all

{$Products->cms->siteprefs->Products_mapi_pref_autopreviewimg|print_r}
Outputs: 1 even though the value is 0

Code: Select all

{$Products->cms->siteprefs->Products_mapi_pref_autopreviewimg->value}
Outputs: nothing

Just for kicks, I tried to access some other variables in the same object.  Many of them behaved the same way, but some outputted the value correctly, like {$Products->cms->db->username}.  This has me completely confused  ???, and I would much appreciate a lesson in smarty / objects  :D.

Versions:
  • CMSMS: 1.6.7 "Teremba Bay"
  • PHP: 5.2.11
  • Apache: 2.0.63
  • Products: 2.4.8
  • CGExtensions: 1.17.8
  • CGSimpleSmarty: 1.4.3

Re: Get and display variables from objects / arrays with smarty - help me?

Posted: Thu Apr 08, 2010 12:30 am
by calguy1000
smarty array elements are accessed like
{$variable.keyname}  or {$variable[99]}

smarty object members are accessed like
{$object->membername}

Re: Get and display variables from objects / arrays with smarty - help me?

Posted: Thu Apr 08, 2010 12:51 am
by jomoweb
Wow, from the module author himself, thanks! For the benefit of others reading, I also see that it you can combine traversers:

Code: Select all

$Products->cms->siteprefs.Products_mapi_pref_autopreviewimg

Good Smarty reference on the subject:
http://www.smarty.net/manual/en/languag ... iables.php