将变量从 PHP 传递给 Smarty


Passing variable from PHP to Smarty

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

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

PHP代码:

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

智能代码:

<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>
这是将数据

从 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}一样聪明地访问它,如果它是数组

您可以使用

以下语法:

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

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

{$result}

希望这对你有所帮助。