- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- About the company

Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Concatenate strings and variables in assignment of new variable in smarty
I want to concatenate an already assigned variable and save it to a new variable, something like this:
So, the resulting $editPerm should be: de.admin.canEditMyClass
How can I do this? Currently, it throws an error: Cannot use string as array offset...
- concatenation
- On which of the four lines the error is thrown? – lorenzo-s Feb 19, 2012 at 17:46
The error you describe cannot be caused by the given code. I assume you are trying to build a string "de.admin.canEditMyClass" to use as a variable {$builtString.foo} . That's where the error occurs, because smarty does not magically convert your string to a variable reference.
If you're using Smarty2:
If you're using Smarty3:
Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged templates smarty concatenation or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- Launching the CI/CD and R Collectives and community editing features for...
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
- The [amazon] tag is being burninated
- Temporary policy: ChatGPT is banned

Hot Network Questions
- Should I use the mean or median of my data for queueing models?
- Nought and Crosses (tic-tac-toe) in C++
- Movie with vikings/warriors fighting an alien that looks like a wolf with tentacles
- Why does hooking voltmeter to two transformers show 0 voltage?
- Voronoi-Lloyd ASCII art
- What sort of strategies would a medieval military use against a fantasy giant?
- Affluence and Morality
- Bulk update symbol size units from mm to map units in rule-based symbology
- QGIS - Countif function
- How to handle missing value if imputation doesnt make sense
- Why does Grover's algorithm behaves so different for different solution addresses?
- Do roots of these polynomials approach the negative of the Euler-Mascheroni constant?
- Why are non-Western countries siding with China in the UN?
- Small bright constellation on the photo
- Is there any way to orbit around the object instead of a 3D cursor?
- Are immutable variables part of the contract storage?
- Getting timestamp of each time pings enter and leave a radius
- How can I make these specific modifications using tcolorbox
- Resistance against timing attacks of AES candidates
- Mini project #2: Guessing a number loop
- Did any DOS compatibility layers exist for any UNIX-like systems before DOS started to become outmoded?
- Coasting Along the Coast
- Why does Jesus turn to the Father to forgive in Luke 23:34?
- Has 90% of ice around Antarctica disappeared in less than a decade?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
Smarty local variable concatenation with string
The way you are doing it is call the "short form" of assign , you just need to use the correct quoting mechanism :
Use assign :
You can also simply put the variables next to one another without assigning it to a variable:
... no variable needed.
A note If you find yourself using assign more than rarely , you might have a misconception about the ideas of separating logic from presentation. Usually, concatenation and other variable work would be accomplished in PHP before the template is ever involved. The template's role is to just display the data , you should avoid creating or altering the data in the template.
Documentation
- Smarty quotes - http://www.smarty.net/docs/en/language.syntax.quotes.tpl
- Smarty assign - http://www.smarty.net/docs/en/language.function.assign.tpl
- Smarty cat - http://www.smarty.net/docsv2/en/language.modifier.cat
And then use the new variable:

How to assign a local template variable with a string concatenated just like below:
to be used below in the code in the manner {$yes_src} .

smarty concatinate variables
Hello all, I need a lttle advice re: joining two string variables together in a .tpl file. Ive tried everything and researched on the googles… but advice so far has all failed to work. Example I just want to join two variables together with a space in between … Eg. variable1 = “one” variable2 = “two” {assign var=“variable3” value=“$one plus $two”} Ive tried: {assign var=“variable3” value=$one $two} {assign var=“variable3” value=$one' '$two} etc nothing works, If I use anything other than just … value=$one} the page load crashes! Could someone please advise me on the correct syntax in cs-cart tpl files . Help greatly appreciated.
[quote name='remoteone' timestamp='1316826896' post='122194'] Hello all, I need a lttle advice re: joining two string variables together in a .tpl file. Ive tried everything and researched on the googles… but advice so far has all failed to work. Example I just want to join two variables together with a space in between … Eg. variable1 = “one” variable2 = “two” {assign var=“variable3” value=“$one plus $two”} Ive tried everything: {assign var=“variable3” value=$one plus $two} {assign var=“variable3” value=$one' plus '$two} etc etc etc nothing works, If I use anything other than just … value=$one} the page load crashes! Could someone please advise me on the correct syntax in cs-cart tpl files . Help greatly appreciated. [/quote] {assign var=variable value=“ $one $two ”} notice the ` around the variables.
[quote name='solesurvivor' timestamp='1316839291' post='122205'] {assign var=variable value=“ $one $two ”} [/quote] Thanks for the fast reply. Yes that worked Ive just realised my mistake, I was using the ' character instead of the ` character. A trap for new players I guess… Cheers
Although reluctant to post php/smarty coding questions on the cs-cart forum, I just havent found any answers in the google kingdom, so since it applies directly to my cs-cart problem, here goes another smart(y) question… Whats the difference between $variable and !$variable ? ie Does {if $lang.variable} mean “if $lang.variable is defined” and conversely {if !$lang.variable} mean “if $lang.variable is NOT defined” ? Cheers
[quote name='remoteone' timestamp='1316941318' post='122250'] Although reluctant to post php/smarty coding questions on the cs-cart forum, I just havent found any answers in the google kingdom, so since it applies directly to my cs-cart problem, here goes another smart(y) question… Whats the difference between $variable and !$variable ? ie Does {if $lang.variable} mean “if $lang.variable is defined” and conversely {if !$lang.variable} mean “if $lang.variable is NOT defined” ? Cheers [/quote] Basically yes. See Smarty documentation {if},{elseif},{else} | Smarty
[quote name='Triplets' timestamp='1316954989' post='122255'] Basically yes. See Smarty documentation {if},{elseif},{else} | Smarty [/quote] Thanks, I had gleaned a hint from that smarty doc, but it doesnt really spell it out… Tried searching for “!$” with no results. Thanks again.
How to combine two strings in smarty?
flossie.kessler

@ flossie.kessler
In Smarty template engine, you can combine two strings by using the concatenation operator, which is represented by a dot ( . ).
Here's an example of how you can combine two strings in Smarty:
Here, $string1 and $string2 are two variables that store the strings you want to combine. The dot operator concatenates the two strings, resulting in a single string.
You can also use the assign function to assign the combined string to a new variable, like this:
In this example, the combined string is stored in the $combined_string variable, which you can then use in your template.
Related Threads:

IMAGES
VIDEO
COMMENTS
The way you are doing it is call the "short form" of assign, you just need to use the correct quoting mechanism: {$yes_src="`$const1`yes`$const2`"} Use assign: {assign var="yes_src" val="`$const1`yes`$const2`"} Use cat: {$const1|cat:"yes"} {$const2} You can also simply put the variables next to one another without assigning it to a variable:
1 Answer Sorted by: 12 The error you describe cannot be caused by the given code. I assume you are trying to build a string "de.admin.canEditMyClass" to use as a variable {$builtString.foo}. That's where the error occurs, because smarty does not magically convert your string to a variable reference. If you're using Smarty2:
To access {assign} variables from a php script use getTemplateVars () . Here's the template that creates the variable $foo . {assign var="foo" value="Smarty"} The template variables are only available after/during template execution as in the following script.
A note If you find yourself using assign more than rarely, you might have a misconception about the ideas of separating logic from presentation. Usually, concatenation and other variable work would be accomplished in PHP before the template is ever involved.
The fact that it is also much easier to concatenate strings in PHP suggests (from a pragmatic perspective) that it is a better way to do it anyhow. Generally, the most complicated logic I have in my smarty code is iteration through arrays.
smarty concatinate variables - posted in Hints & Modifications: Hello all, I need a lttle advice re: joining two string variables together in a .tpl file. Ive tried everything and researched on the googles... but advice so far has all failed to work. Example I just want to join two variables together with a space in between ... Eg. variable1 = "one" variable2 = "two" {assign var="variable3 ...
In Smarty template engine, you can combine two strings by using the concatenation operator, which is represented by a dot (. Here's an example of how you can combine two strings in Smarty: 1
A note If you find yourself using assign more than rarely, you might have a misconception about the ideas of separating logic from presentation. Usually, concatenation and other variable work would be accomplished in PHP before the template is ever involved.