Smarty sub-object referencing - php

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}

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?

Pass several variables from Smarty to PHP function

We got a shopsystem working with Smarty. I need to pass some Smarty variables to a PHP function and get the return.
What I know and also did so far is the following:
{$order_id|#get_order_total}
So this passes the Smarty Variable "$order_id" to a included PHP file which contains the function get_order_total($order_id) and shows me the return of this function.
Now I need to pass 3 variables to a PHP function. The function would for example look like this:
handleDebit($order, $payCode, $insertId)
Sadly i have not found the right thing so far in smarty documentation. Anyone has ever done this?
If you really need to call the function from within smarty templates, register a wrapper function as smarty-plugin:
<?php
$smarty->registerPlugin("function","handleDebit", "handleDebitSmarty");
function handleDebitSmarty($params, $smarty)
{
return handleDebit($params['order'], $params['payCode'], $params['insertId']);
}
Now you can use it as smarty tag:
{handleDebit order=$blah payCode=$blub insertId=$yeahh}
But you should consider #JonSterling s advice and try to find a way auch that a controller is doing the handleDebit-call and you only handle results/display-stuff in the template.

Changing a Smarty value from a plugin function?

I have a plugin function which should be used to modify a given Smarty variable which is an array.
After reading the docs, it looks like this should be the way to do it:
$var = &$smarty->getTemplateVars($params['var']);
$var['blah'] = 'aaa';
... but it doesn't work. The array, as seen by other template code after the call to this plugin function, sees the array unmodified.
So, how can a plugin function modify a template variable?
Unless someone figures out a solution, it looks like it cannot be done in a "function" plugin type. It can be done in a "modifier", though.

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

Assign smarty variable during template time

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

Categories