string(16) "[email protected]" ["realname"]=> string(4) "blah" ["alert_" /> string(16) "[email protected]" ["realname"]=> string(4) "blah" ["alert_" /> string(16) "[email protected]" ["realname"]=> string(4) "blah" ["alert_"/>

error when trying to print associative array element in smarty template

52 Views Asked by At

I have smarty 4.3 and php 8.1. I have this php array:

array(4) {
  ["username"]=>
  string(16) "[email protected]"
  ["realname"]=>
  string(4) "blah"
  ["alert_email"]=>
  string(16) "[email protected]"
  ["okta_username"]=>
  string(16) "[email protected]"
}

and this assignment:

$smarty->assign('user_info', $okta_user);

To reproduce, you would need Smarty 4.3 and this:

<?php

$okta_user = array(
'username' => '[email protected]',
'realname' => 'blah',
'alert_email' => '[email protected]',
'okta_username' => '[email protected]',
);
$smarty->assign('user_info', $okta_user);

$smarty->display('myfile.tpl');
?>

In the smarty template (tpl) file I have this:

<html>
<head>
</head>
<body>
  <table>
   <tr>
     <td>user</td>
     <td>{$user_info.okta_username}</td>
   </tr>
  </table>
</body>
</html>

I get this 500 error in nginx:

PHP message: {"message":"Uncaught Exception TypeError: \"Cannot access offset of type string on string\" at /htdocs/files/templates_c/ce629fe8ee999420d3ca3688e712adb8f5e4083e_0.file.myfile.tpl.php

If I change the line in the table to:

<td>{$user_info}</td>

the page renders and I see this in the table:

{"username":"[email protected]","realname":"blah","alert_email":"[email protected]","okta_username":"[email protected]"}

All the documentation I find, such as this official documentation page https://www.smarty.net/docs/en/language.syntax.variables.tpl, indicate that $user_info.okta_username is the way to refer to this associative array key. I don't understand why I get this 500 error.

0

There are 0 best solutions below