簡體   English   中英

Smarty動態模板包括

[英]Smarty Dynamic Template Include

有沒有辦法動態設置要包含的文件? 當我嘗試時,我收到一條錯誤消息(見下文)。 例如,使用以下模板:

/templates/main.tpl

<!DOCTYPE html>
<html>
    <head>
        <title>My Title</title>
    </head>
    <body>
        {include file='$file'}
    </body>
</html>

這個PHP代碼:

<?php
switch($page){
    case "page1":
         $file = "/templates/page1.tpl";
         break;
    case "page2":
         $file = "/templates/page2.tpl";
         break;
    case "page3":
         $file = "/templates/page3.tpl";
         break;
}
$smarty->assign("file", $file);
$smarty->display("/templates/main.tpl");

然后,Smarty向我拋出此錯誤:

無法將模板文件'$ file'加載到'/templates/main.tpl'中

有沒有辦法動態設置要在該位置使用的模板?

好吧,我知道了!

Smarty不喜歡變量周圍的引號。

因此, {include file='$file'}變為{include file=$file}

暫無
暫無

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

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