How to call smarty variable inside smarty template - php

I use Smarty 3 and I try to extend my links with parameters.
This works fine:
some text
But when I try to extend this link with an existing $_GET param it causes an syntax error and I got a white page.
some text
The $_GET param is like cPath=1_3
Is there another way to call a smarty variable inside smarty?

The solution is very trivial. I need to put the param in double quotes and the smarty call in backticks like
some text

Related

How to use Variable variables in Smarty?

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}

How to use Smarty variables inside of Smarty function calls?

I want to show some value on my site.
The value is declare as:
{$statistics.cashout}
how I can get the value of it, and past it into this:
{fetch file='https://blockchain.info/tobtc?currency=USD&value=$statistics.cashout' assign='btc'}
like this I have try it and do not works...
Thanks
Use the concatenation filter (or are they called something else in smarty? its been awhile):
{fetch file='https://blockchain.info/tobtc?currency=USD&value='|cat:$statistics.cashout assign='btc'}

How to use json_decode with Smarty

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

Smarty local variable concatenation with string

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}

Smarty variables, assigning javascript function as value does not work (upgrading from v2 to v3)

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.

Categories