Passing variable from PHP to Smarty

▼魔方 西西 提交于 2019-12-02 23:53:29

问题


I had two script, one in .php and one in .tpl I need to pass the variable in php to the tpl. I tried this one, but nothinng works (but somehow

  1. it works for one or two days, and after that,
  2. it showed blank,
  3. if i create another php script just to echo the variable, it works.

PHP Code:

<?php
$usdidr2 = "12610.198648";
$usdidr2 =  number_format($usdidr,2,',','.');
echo $usdidr2;
session_start();
$regValue = $usdidr2;
$_SESSION['regUSDIDR1'] = $regValue;
?>

SMARTY Code:

<li>
    <a href="example.php"><strong>
        {php}
            session_start();
            $regValue = $_SESSION['regUSDIDR1'];
            $regValue2 = number_format(45.99*$regValue,2,',','.');
            echo "Rp. ".$regValue."";
            print_r($regValue);
        {/php}
    </strong></a>
</li>

回答1:


Here is the syntax to send data from php to tpl

$smarty->assign('variable name with which you can access the data in tpl', $php_data_you_want_to_send);

Update:

$smarty->assign('rate',$usdidr2);// you just need to write rate without $

You can access it in smarty like {$rate} if it is string You can access it in smarty like {$rate|print_r} if it is array




回答2:


You can use this syntax:

$res = "Hello World!";
$this->context->smarty->assign('result', $res);

And passing to .tpl file like this:

{$result}

Hope this helping you.



来源:https://stackoverflow.com/questions/28316861/passing-variable-from-php-to-smarty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!