简体   繁体   English

Smarty动态模板包括

[英]Smarty Dynamic Template Include

Is there a way to dynamically set a file to include? 有没有办法动态设置要包含的文件? When I try, I get an error message (see below). 当我尝试时,我收到一条错误消息(见下文)。 For example take this template: 例如,使用以下模板:

/templates/main.tpl /templates/main.tpl

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

And this PHP Code: 这个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 then throws this error to me: 然后,Smarty向我抛出此错误:

Unable to load template file '$file' in '/templates/main.tpl' 无法将模板文件'$ file'加载到'/templates/main.tpl'中

Is there a way to dynamically set the template to use at that location? 有没有办法动态设置要在该位置使用的模板?

Okay I figured it out! 好吧,我知道了!

Smarty doesn't like the quotes around the variable. Smarty不喜欢变量周围的引号。

So, {include file='$file'} becomes {include file=$file} 因此, {include file='$file'}变为{include file=$file}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM