簡體   English   中英

Smarty在其中插入PHP函數 <BODY> 而不是里面 <TD>

[英]Smarty inserts PHP function in <BODY> instead of inside <TD>

我在admin.php中有這個簡單的PHP函數

function accountMenu()
{
    if (isset($_SESSION['user_id']))
        { ?>
      <a href="update_profile.php">Update My Profile</a><br>
      <a href="update_email.php">Update My E-mail Address</a><br>
      <a href="logout.php">Logout </a>
    <?php }
}

我在dashboard.php中為該函數分配了一個變量

//smarty paths here

include 'admin.php';

$accountMenu = accountMenu();

$smarty->assign('accountMenu', $accountMenu);
$smarty->display('dashboard.tpl');

並嘗試通過dashboard.tpl顯示它

<body>
    <table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
        <tr>
            <td width="160" valign="top">
            {$accountMenu}
            </td>
            <td width="732" valign="top">
                <h3>Dashboard</h3>
            </td>
        </tr>

        <tr>
            <td colspan="3">&nbsp;</td>
        </tr>
    </table>
</body>

發生的情況是accountMenu元素顯示在<BODY> (甚至在<TITLE>!之前),而不顯示在<TD>

知道為什么會這樣嗎?

您的函數不返回任何內容-它僅將HTML直接輸出到緩沖區,因此在調用此函數時:

$accountMenu = accountMenu();

它立即將其打印到瀏覽器,並且$ accountMenu保持為NULL。

對其進行更改,使其返回所需的字符串,例如:

function accountMenu()
{
    if (isset($_SESSION['user_id'])) return '
      <a href="update_profile.php">Update My Profile</a><br>
      <a href="update_email.php">Update My E-mail Address</a><br>
      <a href="logout.php">Logout </a>
    ';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM