2014-02-07 159 views
0

有沒有一種方法來動態設置文件包含?當我嘗試時,我收到一條錯誤消息(請參閱下文)。例如利用這個模板:Smarty動態模板包括

/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然後拋出這個錯誤對我說:

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

有沒有一種方法來動態設置在該位置使用的模板?

回答

4

好吧我想通了!

Smarty不喜歡變量周圍的引號。因此,{include file='$file'}變成{include file=$file}