Trouble with Smarty Template Engine - php

I am Having Trouble with Smarty.
I need to do some calculations, Assignments, and Deletions in the .tpl file i.e. smarty.
But the problem I am facing is that when ever I do {$idlist[$iSum+1]} It gives Error. here $idlist is an array and $iSum is a an Integer.
It works fine with {$idlist[$iSum]} or {$idlist[3]} but gives error in {$idlist[$iSum+1]}
What must be the syntax to run some statement like this {$idlist[$iSum+1]} ?

Try it this way
{assign var="iSum" value=$iSum+1}
since you also want to use mathematical operation template side, Also read smarty math function

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

smarty assinging variable

I am trying to assing a variable in my .tpl by doing this,
{assign var="image" value="images/stores/{$location.storename|regex_replace:"/[' ']/":"-"|lower}.jpg"}
however I am getting this error,
Smarty error: [in stores/view-store.tpl line 135]: syntax error: invalid attribute name: '|lower'
How Can i stop this error but still drop the casing of the returned info to lowercase?
Even if you take lower off you're still going to have problems. You can't have a {} block inside a {} block. Nor can you have "" nested in "".
http://www.smarty.net/docsv2/en/language.custom.functions.tpl#language.function.assign
Look at that page, check out the complex example. You'll use something like
{assign var="image" value=``}
This might also be useful for you:
http://www.smarty.net/docs/en/language.function.eval.tpl
Ultimately though, you should be doing that on the PHP side, logic and code is not meant to be in the template unless there really is no other choice.

php template engine using %%variable%%

I'm looking at the PHP code for the interspire shopping cart and they make extensive use of template variables such as %%GLOBAL_variables%% and %%variable%%.
I haven't seen those before and I'm trying to understand how they are defined and used. Does anyone know what template engine is involved and any documentation on it?
thanks
I've used %% as "delimiters" for my own homegrown templating engine. There is nothing special about it, they are just characters that will prevent any unwanted replacements since it is very unlikely they will occur naturally. Some engines use {keyword}, like Smarty.
As an example, you can do a quick search/replace with an associative array of data.
$data_replace = array('%%GLOBAL_variable%%'=>'some data',
'%%variable1%%'=>'different data',
'%%variable2%%'=>'limited time only!');
//Perform the search and replace
$output = str_replace(array_keys($data_replace), $data_replace, $template_text);
As a guess it looks like a home grown solution.
That said, it would be pretty easy to re-create something like this by doing something like:
Load the template file into a
string.
Grab all occurances of '%%xxx%%'.
(I'm guessing the '%%' are just
handly delimiters.)
Convert the first '%%' to '$_' if
the occurance begins with '%%GLOBAL'
or '$' otherwise.
Once all that's done evaluate the resultant string. (eval)
Additionally, it would be possible to include variables within the scope of the evaluation using extract.
Irrespective, I'd have thought you should be able to confirm this by having a hunt around in the code.

Categories