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}.
Related
$newOrders is an array and contains order objects...
order_id is an objects variable. I want to compare the order_id value to another variable($orderId) in If loop...
but it fails
Here is my code:
if($newOrders[$i]->order_id == $orderId){
echo "voila, found it:".$newOrders[$i]."<br>";
return $newOrders[$i];
}
Whenever I come across a piece of code that does not work - especially comparisons - I print out both sides of the variables (and often break down more complex variables, like your array) and actually look at the information, rather than assume I know from looking at the code.
It is inevitably a problem that is obvious as to what is wrong when the data is dumped out or otherwise manually examined. Tools such as Symfony VarDumper or just print_r, or an IDE with breakpoints and variable inspection are all suitable to see exactly what is going on.
Are you sure this array contains object, have you checked? If yes, then how ?
What is the variable $i there, could you please put full code (I believe snippet should be in some loop for or foreach)
You can always check for a valid object of a class by
if ($newOrders[$i] instanceof Order) { //Presuming Order is your class name
//do your stuff
}
You can also check by using var_dump() function to check the variables inside the object.
I hope it'll help.
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.
We have this Smarty function that returns HTML code for templates. However it is also possible that the function returns a null string, which we now wish to identify. Our system has been running stably for years, so I am looking for the least invasive possible solution.
Is it possible to assign the return value to a smarty variable? I have tried assigning it to a Javascript variable, however, because part of the HTML is user generated, the return string could be a mixture of double and single quotes, which causes problems in IE (unfortunately the majority of our user base).
<script type="text/javascript">
var html = '{smarty function}'; //IE chokes on mixed quotes
</script>
Any help appreciated!
Use escape modifier, for example:
{$variable|escape:'quotes'}
For smarty function, you can first try if {smarty_function|escape:'quotes'} works, if it doesn't then you have to assign the output of the function into a variable first before escaping it, and for that you use capture:
{capture name=mycapture}{smarty_function}{/capture}
{$smarty.capture.mycapture|escape:'quotes'}
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.