2

I use a shopsystem that got updated from smarty 2 to smarty 3, which led to a list of problems on our site. The worst one is that all the Smarty variables we assigned to use them in PHP don't work anymore.

A short example:

{assign var=test value=$ORDER_NUMBER}
{php}
    $order = $this->get_template_vars('test');
    echo $order;   
{/php}

This results in following error:

FATAL ERROR(1): "Using $this when not in object context"

Now on the Smarty Page i found some lines of code that do the same but look a bit different, for example this one:

$order = $smarty->getTemplateVars('test');

which results in:

FATAL ERROR(1): "Call to a member function getTemplateVars() on null"

None of all these "solutions" i found work anymore since Smarty 3.

Marcel Wasilewski
  • 2,519
  • 1
  • 17
  • 34
  • `get_template_vars()` is deprecated use `getTemplateVars()` and make sure before this line: `$smarty->getTemplateVars('test')`that you had initialized your smarty object. – Amani Ben Azzouz Oct 07 '15 at 08:41
  • The `php`-tag is deprecated in smarty3 also, you should use the available possibilities to extend smarty to replace your usage of `php`-blocks with custom blocks/functions/... – Tom Regner Oct 07 '15 at 08:44
  • Your second method should work using the $smarty variable. If not perhaps try putting global $smarty; before your code. However your code is bad and very out of date and needs to be changed over time. In the next version of smarty it probably wont work at all. – Toby Allen Oct 08 '15 at 16:55

1 Answers1

0

For whatever reason, none of the things above worked. I now outsourced the code in the {PHP}-tags in an external PHP file and then get the return of the function back to smarty.

Below i got an example for anyone struggling with it (don`t forget to include your PHP file to your index.php or whatever):

I want to get some information about a product out of the database but only have the {$module_data.PRODUCTS_ID} given in smarty.

So in my file where i use smarty tag i send this variable to my function in PHP: {$module_data.PRODUCTS_ID|@get_random_function}

Then in my external PHP file i do my PHP function and return the data i need:

function get_random_function($products_id)
{
    $t_sql = $sql = "SELECT * FROM products WHERE products_id='".$products_id."'";
    $retval = mysql_query($t_sql);
    $row = mysql_fetch_assoc($retval);


    return $row['gm_needed_data'];
}

Now {$module_data.PRODUCTS_ID|@get_random_function} outputs me the return of my PHP function.

Marcel Wasilewski
  • 2,519
  • 1
  • 17
  • 34