I am working on a PHP project where I have an array of objects. I want to pass this array of objects to a Smarty template that can loop round and get the information out of the structure.
An example output of the object array is as follows:
Array
(
[0] => FilesAndDirectories Object
(
[parent] =>
[directory] => /
[files] => Array
(
[0] => FileInformation Object
(
[fileName] => test_wav.wav
[modifiedTime] => 2014-09-11T19:20:20.000Z
[fieSize] => 22332
)
[1] => FileInformation Object
(
[fileName] => record_message_from_sip:300@sip-0-2-0.aculab.com.wav
[modifiedTime] => 2014-09-11T21:01:39.000Z
[fieSize] => 36036
)
[2] => FileInformation Object
(
[fileName] => record_message_from_sip:44123456789@sip-0-2-0.aculab.com.wav
[modifiedTime] => 2014-09-11T21:07:05.000Z
[fieSize] => 29796
)
)
)
[1] => FilesAndDirectories Object
(
[parent] => /
[directory] => my_directory_test
[files] => Array
(
)
)
[2] => FilesAndDirectories Object
(
[parent] => my_directory_test
[directory] => dir
[files] => Array
(
[0] => FileInformation Object
(
[fileName] => /record_message_from_sip:300@sip-0-2-0.aculab.com.wav
[modifiedTime] => 2014-09-12T20:36:05.000Z
[fieSize] => 44596
)
)
)
)
Below is how I am getting the above output stored
$fileManager = new FileManager();
$result = $fileManager->getFileListing();
$result will now contain the above structure.
I am then passing $result to assign to a smarty variable as follows:
$smarty = new Smarty();
$smarty->setTemplateDir("templates");
$smarty->assign("fileListing", $result);
echo $smarty->fetch("directoryListing.tpl");
The smarty template file looks like the following:
<table>
{foreach $fileListing as $directory}
<tr>
</tr>
{/foreach}
</table>
The problem I am having is that I get an exception thrown via Smarty which is
Catchable fatal error: Object of class FilesAndDirectories could not be converted to string in
Use assign_by_ref()
instead of assign()
or check one of the presented here examples.