Merge strings and variables when assigning a new variable in smarty

I want to concatenate an already assigned variable and save it in a new variable, something like this:

{assign var=permCat value="de.admin"}
{assign var=objectName value="myClass"}
{assign var=objectNameUpper value=$objectName|ucfirst}
{assign var=editPerm value=$permCat|cat:"canEdit"|cat:$objectNameUpper}

So, the result $editPermshould be:de.admin.canEditMyClass

How can i do this? He is currently throwing an error:Cannot use string as array offset...

+5
source share
1 answer

The error you described cannot be caused by this code. I assume that you are trying to build the string "de.admin.canEditMyClass" to use as a variable {$builtString.foo}. This is where the error occurs, because smarty does not magically convert your string to a variable reference.

If you are using Smarty2:

{assign var=objectName value="myClass"}
{assign var=objectNameUpper value=$objectName|ucfirst}
{assign var=editPerm value="canEdit"|cat:$objectNameUpper}
{$de.admin.$editPerm.foo}

If you are using Smarty3:

{$de.admin.{"canEdit"|cat:{"myClass"|ucfirst}}.foo}
+12
source

All Articles