Assign smarty variable during template time - php

In a smarty template I call a user defined function, as a modifier like that:
{"myArray"|assignArray}
my user defined function in php looks as:
function smarty_modifier_assignArray($str)
{
global $smarty;
if ($str=="myArray")
{
// it is not constant in real, but comes from a mysql query
$all = array( array("foo","joe")), array("green", "blue"));
$smarty->assign($str,$all);
}
return null;
}
My purpose is that loading "myArray" from mysql is expensive, and if my template do not need that
array, I don't want to load it. My template follows as:
{"myArray"|assignArray}
{foreach from=$myArray item=r}
{$r[0]}
{/foreach}
The problem is, that in the foreach I can't see $myArray (or it is empty). I read in the smarty forum (v3.x, http://www.smarty.net/forums/viewtopic.php?p=77671 ) that for speed optimalization the variables are copied to the template space, so after starting a template, one can not assign new variables onto that. Sadly it seems true. I suppose using template {assign ...} I still could do that, but I was not able to generate dynamical multi-level arrays into that {assign ...} :(
What should I do? Any ideas?

You can assign smarty multilevel array while rendering,
use {assign} tag for it.
Syntax,
{assign var=foo value=[1,[9,8],3]} // can be nested

Related

Passing a smarty array value to php object...?

In Smarty: is it possible to call a PHP function (from the controller class) inside the template? there is explained, how to call class methods out of template files.
You nee to assign the object like
$smarty->assign('a', new Controller);
and you can use it like
{$a->foo(5)}
But what, if I want to pass a smarty array value as parameter? It should be something like this:
{foreach from=$dataset item=data}
{$a->foo($data.id)}
{/foreach}
for sure, this won't work. But how can this issue be handled?
Chapter 15. Advanced Features
This is what you need?

Assigning array variable in smarty

I'm quite new to smarty template in php. I want to assign a value to a variable inside the smarty template:
{assign var=all_person_exams value={$gdao->getGrade({$val->getTotal()},{$smarty.session.schoolsection})}
The value returned by the method $gdao->getGrade(...) is an array. This method has been thoroughly tested without smarty. But, it's not working here. How do i go about it?
Note: it should have returned a value when i do something like: {$all_person_exams.grade}
Try
{assign var=all_person_exams value=$gdao->getGrade($smarty.session.schoolsection)}
Smarty assign
or even
{$all_person_exams=$gdao->getGrade($smarty.session.schoolsection)}
Smarty short form assign

Using plugin functions in Smarty foreach

I've been experimenting with Smarty lately a bit (first time into using this kind of stuff), and I have a quick question I just can't figure out..
I have created a function for Smarty, called get_users(), so it'd be {get_users} into my .tpl
I want to do a foreach of these "get_users", so it'd look like this
{foreach get_users as $user}
magic
{/foreach}
Now, my question is.. as this is not working, how should I approach this issue?
Thanks!
You probably should use $smarty->assign(...) inside the function to return the result in a variable and then write something like:
{get_users var=user_list}
{foreach $user_list as $user}
....
{/foreach}
read http://www.smarty.net/docs/en/plugins.functions.tpl
First, your plugin should assign the users variable to the template before iterating over it. This can be done like this :
function smarty_function_get_users($params, $smarty)
{
..... // your stuff goes here
$users = array(); // get your users data here
$smarty->assign($params['users'], $users);
}
Then you can iterate over it like this :
{get_users users=users}
{foreach from=$users item=user}
{$user}
{/foreach}

PHP Foreach in SmartyBC. Smarty template

I'm relatively new to Smarty and can't seem to figure this out.
In my php controller I instanciate a class "Product" as many times as I will need to display it. For instance if I have 5 different items on my page I will instanciate "Product" 5 times. I then create an array containing these. It goes something like this :
(my objects are filled with infos on the product).
Code:
$product_array = array (
0 => Object1,
1 => Object2); etc.
I then assign that array to a smarty variable {$product_array}.
Once in my .tpl I go through a loop of the displayed products. That's when I would need to assign $products_array[0] or {$products_array.0} to another variable $products that would display everything I need about the product. Then it will go through the loop again and show me {$products_array.1} and so on.
However this seems impossible seeing that any way I try and achieve this (and I even tried using {php} shame on me) I can't seem to figure it out.
I get this error : Object of class Product could not be converted to string.
Im pretty sure you cant mix in smarty stuff within a {php} tag. But the questions is why do this? Why not create all the products and put them in an array, and then assign that to smarty and loop over it in smarty syntax?
{php}
$accesories = $GLOBALS['accessories'];
$products = array();
foreach($accesories as $k => $v)
{
var_dump($instantProduct = new Product($accesories[$k], TRUE));
$products[$k] = new Product($accesories[$k], TRUE);
}
$smarty->assign('products', $products);
{/php}
// now loop and conditions in smarty
That said you really should just fix this now and move this logic into whatever youre using as a "controller". It may reveal more widespread problems with your design that you are going to have to solve one way or another.

Smarty sub-object referencing

In smarty I'm having to load data from an object using a function ala: $obj->Function1(1) I then want to be able to acces a value that function returns like this: $obj->Function1(1)->name but get this error "unrecognized tag: $cat->getSubCategories(1)->name " is there an easy way of getting this done?
You may have to assign the result of Function1() to a variable. It's not pretty but it should work.
{assign var='foobar' value=$cat->getSubCategories(1)}
{$foobar->name}

Categories