将变量从PHP传递给Smarty

时间:2015-02-04 08:36:36

标签: php variables smarty

我有两个脚本,一个在.php中,另一个在.tpl中 我需要将php中的变量传递给tpl。 我尝试了这个,但是nothinng工作(但不知何故

  1. 它可以工作一两天,然后
  2. 显示空白,
  3. 如果我创建另一个 php脚本只是为了回显变量,它就可以了。
  4. PHP代码:

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

    SMARTY代码:

    <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>
    

2 个答案:

答案 0 :(得分:1)

以下是将数据从php发送到tpl

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

<强>更新

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

如果它是字符串,您可以像{$rate}那样聪明地访问它 你可以像{$rate|print_r}那样聪明地访问它,如果它是数组

答案 1 :(得分:0)

您可以使用以下语法:

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

并像这样传递给.tpl文件:

{$result}

希望这对你有所帮助。