• 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...

F.P's user avatar

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:

rodneyrehm's user avatar

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 .

Hot Network Questions

smarty assign string concat

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

And then use the new variable:

algorytmus

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} .

rodneyrehm

smarty assign string concat

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

Facebook

 @ 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

  1. String concat() in java with example

    smarty assign string concat

  2. String concat() in java with example

    smarty assign string concat

  3. String concat() method in JavaScript

    smarty assign string concat

  4. assign in smarty

    smarty assign string concat

  5. String concat() in java with example

    smarty assign string concat

  6. JavaScript concat String

    smarty assign string concat

VIDEO

  1. i do smarty pie string clutch 😈#shorts

  2. Value Toggle Examples

  3. String or text functions in Microsoft Excel (in Hindi)

  4. CONCAT STRING METHOD IN JAVASCRIPT. #javascript #shorts

  5. 3 Nested Querie

  6. Dynamic SQL, String CONCAT, Recovery Models شرح بالعربي

COMMENTS

  1. php - Smarty local variable concatenation with string - Stack ...

    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:

  2. Concatenate strings and variables in assignment of new ...

    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:

  3. {assign} | Smarty

    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.

  4. [Solved] Smarty local variable concatenation with string

    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.

  5. Smarty :: View topic - concatenate strings

    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.

  6. smarty concatinate variables - Hints & Modifications - CS ...

    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 ...

  7. How to combine two strings in smarty? - devhubby.com

    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

  8. Smarty local variable concatenation with string - CodeForDev

    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.