I have this:
string {$one} = "$hello_"
string {$two} = "world"
How can I call the variable $hello_world from the above two string variables?
capture did not work for me.
Uses Smarty v2.5
{${$foo}{$bar}} will only work in Smarty3, though. In Smarty2 you'd have to write a plugin for that (or simply search the smarty forum, as there are plenty of solutions thereā¦)
From the documentation:
{$foo_{$x}} // will output the variable $foo_1 if $x has a value of 1.
So, you want:
{${$one}{$two}}
Since this functionality isn't allowed, I would recommend using a smarty plugin to mimic the behavior you want. Template plugins are just simple php functions, called via the $smarty->loadPlugin() method.
Smarty 2.x doesn't support variable variables.
Variables in smarty are actually stored inside the smarty object so you'd need explicit support in Smarty to use the convenient standard variable-variable syntax.
The following is the best I could come up with in Smarty 2.x. It uses a PHP block to store the value of the combined result for you.
{assign var="one" value="hello_"}
{assign var="two" value="world"}
{assign var="hello_world" value="HELLO!"}
{php}
$varname = $this->get_template_vars('one').$this->get_template_vars('two');
$this->assign('result', $this->get_template_vars($varname));
{/php}
{$result}
As mentioned, however, you have to get rid of the $ at the beginning of the value of $one. Otherwise you will need to modify it to the following line:
$varname = substr($this->get_template_vars('one'),1).$this->get_template_vars('two');
Related
I want to use variable variables in smarty. I know that we can use it in PHP. But I am unable to find any way to achieve the same in Smarty template file. Consider the below scenario.
I have multiple variables which I pass from PHP file to Smarty tpl file. All these variables name have some similar pattern. e.g. $test_1, $test_2, $test_3 and so on.
This is how, actually I am trying to achieve it. Here, $COUNTER represents 1, 2, 3....
{$SELECTED_VALUE = "test_{$COUNTER}"}
{$$SELECTED_VALUE|#print_r}
But when I try to print it out, it gives me error
Syntax Error in template "test.tpl" on line 127 "{$$SELECTED_VALUE|#print_r}" - Unexpected "$", expected one of: "{" , "identifier"
Now, in PHP, I can get the values of these variables using double $$ symbols. But I am unable to find any way to achieve the same in a smarty tpl file.
I have gone through these links, but couldn't understand anything out of it.
Variable Variable in Smarty Templates
Dynamics variables in smarty in loop
Kindly guide me here, if it is possible.
Ok, it seems that I have found the solution. As I have mentioned above, I have these dynamically created and assigned $test_1, $test_2, $test_3,.... to smarty tpl file. So to use these variables dynamically, I have taken followed approach.
{for $counter=1 to $total}
{$test_{$counter}}
{/for}
Thanks for help.
you can write a loop in your tpl file below
For example, lets say $count = 10;
{for $foo=1 to $count}
<li>{$foo}</li>
{/for}
This should do what you are looking for:
{$x = ['foo', 'bar']}
{${$x}|print_r}
Its my first time working with Smarty Template Engine and I am having troubles translating the following PHP json_decode statement into a Smarty friendly code. The output is an array since I use the TRUE in the json_decode.
$itemArray = json_decode($dni-content-slider-id-prefix-saved-content-picker-item- . $promos, true)
I've tried
{ assign var $itemArray = value=$dni-content-slider-id-prefix-saved-content-picker-item-|cat:$promos|json_decode}
But it doesnt really work. any suggestions?
Pretty much everything in that line of Smarty is wrong; let's go through in order:
{ assign
Don't put a space between the { and the Smarty tag; if Smarty 3's "auto-literal" feature is switched on, it will assume that's a literal { not a Smarty tag.
var $itemArray =
You seem to be mixing two different functions here. The Smarty {assign} function takes the form {assign var=some_name value=$some_value}. Note that the var parameter is the name of the variable to assign, so does not need a $.
Smarty 3 also has a PHP-style "short-form assign", which looks like {$some_name=$some_value} (complete with $, but no assign keyword).
value=$dni-content-slider-id-prefix-saved-content-picker-item-|cat:$promos
This will take the content of the variable $dni-content-slider-id-prefix-saved-content-picker-item- treat it as a string, and add the content of the variable $promos (also treated as a string) to the end of it. Looking again, I see that that is also what your PHP code does, but it seems a very odd thing to do, since the first variable would have to be something like "{'foo':'bar'," and the second something like "'baz':'quux'}". Why would you ever have variables like that?
Based on the PHP code being the same, I'm going to assume this paragraph was wrong on my part What I suspect you want is a variable variable name (a different variable when the code runs); there are ways to do that, but this isn't one of them. It's also generally a really bad idea; if you want lots of similar variables which you can select from at run-time, put them in an associative array, and index something like $dni-content-slider-id-prefix-saved-content-picker-items[$promos].
|json_decode}
Finally, the part actually related to your question. You point out that in PHP, you pass the optional true parameter to the json_decode function, but in this Smarty code, you are not doing so. This can be easily done by adding :true on the end, as in {assign var=itemArray value=$jsonString|json_decode:true}.
How to assign a local template variable with a string concatenated just like below:
{$yes_src=const1.'yes'.const2}
to be used below in the code in the manner {$yes_src}.
By the way I am looking for a job as PHP developer :)
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:
{$const1}yes{$const2}
... 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
{ $yes_src = $variable|cat:"some string"|cat:$variable }
Try this:
{capture assign=yes_src}{$const1}.'yes'.{$const2}{/capture}
And then use the new variable:
{$yes_src}
I have the following code, that works for smarty 2.x
{assign var=somename value=jsFunction($frontItemKey);}
but smarty v3 throws an error:
unknown function "jsFunction"
How can I fix this?
Thanks!
What are you trying to achieve, to assign a string "jsFunction($frontItemKey)" to a variable? Or to put there the return value of some function?
In first case, which seems more possible, I think you just need should handle it as a string, because Smarty is definitely trying to call a function by that name and can't find it.
If you want a string like "jsFunction(VALUE)" where value is the $frontItemKey value, you should concatenate it.
In second case, if Smarty2 puts some value there, I would first check your Smarty2 source code, probably somebody changed it and added this function. Take a look at the Smarty libs folder.
Say I have the following in my TPL file:
{$a}
and I want to apply certain PHP native functions (e.g. strip_tags) to that Smarty variable. Is this possible within the TPL? If so, how?
You can use any php function in a smarty template in the following way:
{$a|php_function_name}
or
{$a|php_function_name:param2:param3:...}
In the second example you can specify additional parameters for the php function (the first is always $a in our case).
for example:
{$a|substr:4:3} should result something like substr($_tpl_vars['a'],4,3); when smarty compiles it.
The best way is probably to create your own plugins and modifiers for Smarty. For your specific example, Smarty already has a strip_tags modifier. Use it like this:
{$a|strip_tags}
Very good question, it took me a while to completely figure this one out.
Call a function, passing a single parameter:
{"this is my string"|strtoupper}
// same as:
strtoupper("this is my string")
{$a:strtoupper}
// same as:
strtoupper($a)
Call a function, passing multiple parameters
{"/"|str_replace:"-":"this is my string"}
// same as:
str_replace("/", "-", "this is my string")
{"/"|str_replace:"-":$a}
// same as:
str_replace("/", "-", $a)
Or you can use this: (call function directly)
{rand()}
The whole point of templating systems is to abstract the creation of views from the underlying language. In other words, your variables should be prepared for displaying before they are passed to a templating engine, and you should not use any PHP functions in the template itself.
Smarty already has a Language Modifier built in for this.
{$a|strip_tags}
You don't need Native functions as there already integrated into the plugin system
http://www.smarty.net/docsv2/en/language.modifier.strip.tags.tpl
others here:
http://www.smarty.net/docsv2/en/language.modifiers.tpl