smarty assinging variable - php

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.

Related

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

Trouble with Smarty Template Engine

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

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.

php code, is it slow or wrong?

"multiple annotations found at this line"
i have been using aptana and other IDE before, but never gave me this error, but yesterday i installed zend studio and it was giving following error in all my code where i have assigned and also check if condition at same time.
code:
line 16: if ($message_array = #unserialize($e->getMessage()))
line 17: $message = $message_array;
on all if condition, where i assigned the value to a variable and also check if the variable is true/false, it gives me error "multiple annotations found at this line"
Yeah, that syntax is typically flagged by most decent IDEs as "Accidental Assignment" (since it's not obvious if you meant = or ==). Most will allow you to wrap it in a () to silence the error (since then it's apparent you want the result rather than a test):
if (($message_array = #unserialize($e->getMessage()))) {
}
Also, for readability and maintainability, I would suggest a few things there.
First, use braces. Since it's only a special case that lets you not use them, I personally think it's better form to always use them so that it's clear what was meant.
Second, do all asignment outside of the if clause. It makes it more explicit and easier to tell at a quick glance what you meant. Plus it looks less cluttered.
$message_array = #unserialize($e->getMessage());
if ($message_array) {
...
}
Third, I would suggest avoiding the # operator. To me it's a sign of code-rot. While I know it's easy to use and easier than properly handling the error, I think it's just a short-cut that will make life harder for you. You can avoid it in a few ways. First, you can check the string before you pass it to unserialize. Make sure it's non-empty, a string, etc. Secondly, you can install an error handler to throw exceptions on PHP errors (what I do). That way, you just wrap the unserialize call in a try {} catch(){} block. It's nicer since you actually can inspect the error rather than just trusting that the thrown error is what you think it is...
That is not an actual error. Instead the IDE has found more than one error, warnings or hints at the same code line.
You can look at the Problems tab to see the actual errors and warnings.

Categories