Using plugin functions in Smarty foreach - php

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}

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?

Make this Magento nested loop better

I am trying to get all stores on a Magento shop. By all stores, I mean all stores from all websites. I wrote this code and it works, but I'm a little concerned about the complexity of the nested foreach loop. Please take a look at it and advise me if you think I can do something different.
public function getAllStoresCustom(){
$all_stores = array();
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$all_stores [] = $group->getStores();
}
}
return $all_stores;
}
I've only found these functions in Magento, so I think I had to use those and this seemed the only combination that worked.
Thanks a lot
Try this:
$allStores = Mage::getModel('core/store')->getCollection();
Then loop through $allStores when needed
foreach ($allStores as $store) {
//do something with $store
}
Notice: You will get a store with id 0. That is the admin store view. If you want all stores without the admin store view use this:
$allStores = Mage::getModel('core/store')->getCollection()->setWithoutDefaultFilter()
foreach() is an incredibly efficient PHP function so you can bet that it is not going to be your slowdown. If you are looking to optimize something then look into the code for the getStores() and getGroups() functions because those are being called within the iterations whereas getWebsites() gets called only once.
If you want more guidance then please feel free to update your question with the contents of those functions.
You may also want to try https://codereview.stackexchange.com/ for more experienced opinions especially since you don't have any specific programming issue/error =)

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

Trying to call a smarty plugin (with params) in smarty foreach

I'm trying to embed a personal plugin into my smarty TPL files, but I couldn't make it work...
This is my smarty plugin:
<?php
function smarty_function_alticerik($params, &$smarty) {
if (!function_exists('alticerik')) {
if (!function_exists('get_instance')) return "Can't get CI instance";
$CI= &get_instance();
}
return $CI->IceriklerMod->liste($params['where']);
}
?>
And these are my jabber wocky TPL codes:
{foreach item=alt from=alticerik|#alticerik(where="where ustid=$ustid")}
{$alt.id}
{/foreach}
I have searched and read all of the smarty help pages but I still have no idea that how can I make this codes work correctly...
I believe your issue is that functions don't get called using that Smarty syntax.
What you're doing is sort of a mix between a function and a modifier.
Modifiers change a given input - for example, lower casing a string.
{$upperCaseWord|strtolower}
Functions take named parameters and usually do a bit more work such as creating a dropdown.
{html_options options=$arrayOfOptions selected=$selectedValue}
In your case, I'm guessing that you want to use a modifier since you look to be attempting to modify a value. You can still pass options into those, but they aren't named and it gets fairly confusing quickly. However, the code might be:
{foreach item=alt from=$alticerik|#alticerik:"where ustid=$ustid"}
{$alt.id}
{/foreach}
Meanwhile your actual function looks like:
<?php
function smarty_modifier_alticerik($input, $whereString) {
// Here, $input is the same as your $alticerik variable
// and $whereString is just the string that comes after the colon.
if (!function_exists('alticerik')) {
if (!function_exists('get_instance')) return "Can't get CI instance";
$CI= &get_instance();
}
return $CI->IceriklerMod->liste($whereString);
}
?>
Note, however, that in your code, you don't end up using the value of $alticerik from your template, so it makes me wonder if you need a function instead. I can't really know for sure unless I get what the alticerik plugin is supposed to do.
For more information on functions and modifiers, see the documentation here: Smarty.net Function Documentation and here: Smarty.net Modifier Documentation.
Do you mean $CI =& get_instance(); perhaps?
and what's not working? Any errors?

Categories