Search code examples
phpsmarty

Smarty array with dot in key


I have an array in PHP that looks like this:

$config['detailpage.var1']
$config['detailpage.var2']
$config['otherpage.var2']
$config['otherpage.var2']
...

To access it in Smarty I would do

$smarty->assign('config', $config);

With this template:

{$config.detailpage.var1}

Unfortunately this does not work, due to the dot in my array key "detailpage.var1", which for Smarty is the delimitor for the array elements. As I don't want to rewrite my config array (cause it is used in many other places), my question is:

Is there any other notation I could use that works with the dots in the array keys? Or can I somehow escape them?


Solution

  • I've "accidentally" found the answer to this question after I was looking myself for an answer here. In this case I'm using hostnames as keys which always have dots. You can access them with {} around the dotted key name. e.g. {$var.foo.bar.{"my.hostname.example.com"}.ipaddress} .

    This "escaped" syntax can also be used in cases where you have to expand a variable that contains a dot. e.g. {$var.foo.bar.{$var.bingo}}