简体   繁体   中英

Assign Smarty variable to PHP with Smarty 3

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM