5

Hi i have written a function in .php file. i.e.

public static function getCategories($id_lang = false, $active = true, $order = true, $sql_filter = '', $sql_sort = '',$sql_limit = '')
{
    if (!Validate::isBool($active))
        die(Tools::displayError());

    $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
        SELECT *
        FROM `'._DB_PREFIX_.'category` c
        LEFT JOIN `'._DB_PREFIX_.'category_lang` cl 
        ON c.`id_category` = cl.`id_category`
        WHERE 1 '.$sql_filter.' '.($id_lang ? 'AND `id_lang` = '.(int)($id_lang) : '').'
        '.($active ? 'AND `active` = 1' : '').'
        '.(!$id_lang ? 'GROUP BY c.id_category' : '').'
        '.($sql_sort != '' ? $sql_sort : 'ORDER BY c.`level_depth` ASC, c.`position` ASC').'
        '.($sql_limit != '' ? $sql_limit : '')
    );

    if (!$order)
        return $result;

    $categories = array();
    foreach ($result AS $row)
    {
        $categories[$row['id_parent']][$row['id_category']]['infos'] = $row;
    }
    return $categories;
}

and i want to call this function inside a .tpl file. I used {php} {/php} way,but this not works. What is the way to call this one?

Thanks

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
srinu
  • 259
  • 3
  • 7
  • 14
  • This function returns an array of categories, am I right? Ok, so, what do you want to do with this array? How you need to use it in your Smarty template??? – lorenzo-s Dec 29 '11 at 09:47

4 Answers4

6

Smarty is a templating language - if you want to output the results of a function, assign the output to a smarty variable

$smarty->assign('categories', getCategories($args));

And then use it in your template

{$categories}

There are very few situations where this isn't the correct pattern.

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • thanks for the reply adam. I written the code above you said. but it wont works. can u plz specify where i have to write the line $smarty->assign('categories',getCategories($args)); – srinu Dec 29 '11 at 06:26
  • I agree with this comment. See a [related answer on SE](http://stackoverflow.com/a/7446889/913758). – NobRuked Dec 29 '11 at 10:25
  • @srinu the first line goes in your php, the second in your template. Have you even read the manual? http://www.smarty.net/documentation – Adam Hopkinson Dec 29 '11 at 12:17
  • I was written like what you said, but it was also not showing categories. – srinu Jan 02 '12 at 13:33
5

you can use something like this and i always used it

{category.id|function_name}

let me explain it :

category.id = is the id of the category you want to get information about in the function

function_name = is the name of the function

  • That would only work with the built-in Smarty functions, not with the custom function defined in the question – Adam Hopkinson Sep 28 '13 at 17:52
  • 1
    i always use it like this function Test(){ echo "Hello World"; } in HTML smarty i use it like this after i include the function in the php page {$a|Test} Output will be Hello World – Usf Ahmed Subehi Sep 30 '13 at 21:21
2

because you are using a static method, you will not be able to use $this keyword in your function. So therefore, you will not be able to use

$this->context->smarty->assign('categories', self::getCategories($args));

So you should create a variable and assign the context to it and in your case you will only need the smarty part of it.

$smarty = Context::getContext()->smarty;

Now you can use this as follows:

$smarty->assign('categories', self::getCategories($args));

I'm assuming that you will call the function in the same class, so I used self::getCategories($args), if you want to use it in another class use className::getCategories($args)

Hope this helps you, try it and give me feedback! ;)

0

I agree with adam, though somitemies I use the php inside smarty due to given architecture.
There is another way to do it: with {insert} -http://www.smarty.net/docs/en/language.function.insert.tpl

lvil
  • 4,326
  • 9
  • 48
  • 76