Php 为什么smarty模板引擎获胜';找不到我的模板文件?
为什么smarty模板引擎找不到我的模板文件 致命错误:未捕获-->Smarty:无法加载模板文件 'test.tpl'testInstall()代码> 和Php 为什么smarty模板引擎获胜';找不到我的模板文件?,php,smarty,Php,Smarty,为什么smarty模板引擎找不到我的模板文件 致命错误:未捕获-->Smarty:无法加载模板文件 'test.tpl'testInstall() 和var_dump($smarty->getTemplateDir()) 数组(1){[0]=>字符串(55) “C:/xampp/htdocs/testing/templates/frontend/default/tpl\”} 文件模式 htdocs -- testing -- incluses --
var_dump($smarty->getTemplateDir())代码>
数组(1){[0]=>字符串(55)
“C:/xampp/htdocs/testing/templates/frontend/default/tpl\”}
文件模式
htdocs
-- testing
-- incluses
-- smarty
plugins
sysplugins
Smarty.class.php
SmartyBC.class.php
-- configs
configs.php
-- db
connect.php
db.php
-- configs
-- cache
-- templates
-- frontend
-- default
-- css
-- mages
-- js
-- tpl
test.tpl
-- backend
-- templates_c
-- frontend
index.php
index.php
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('display_startup_errors', TRUE);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
ob_start();
session_start();
require 'includes/smarty/Smarty.class.php';
require 'includes/db/db.php';
require 'includes/configs/configs.php';
$page = isset($_GET['do']) ? $_GET['do'] : '';
switch($page){
case 'home';
include 'pages/home.php';
break;
default:
include 'pages/test.php';
break;
}
ob_flush();
?>
configs.php
<?php
$smarty = new Smarty();
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->cache = 1;
$smarty->setTemplateDir('C:/xampp/htdocs/testing/templates/frontend/default/tpl');
$smarty->setCompileDir('C:/xampp/htdocs/testing/templates_c/frontend/default');
$smarty->setCacheDir('C:/xampp/htdocs/testing/cache');
$smarty->setConfigDir('C:/xampp/htdocs/testing/configs');
?>
test.php
<?php
//$success = 'Success Message';
//$error = 'Error Message';
$errors[] = 'Error one';
$errors[] = 'Error two';
$smarty = new Smarty;
//$smarty->assign('success', $success);
//$smarty->assign('error', $error);
$smarty->assign('errors', $errors);
$smarty->display('test.tpl');
?>
test.tpl
{if !empty($errors)}
<div id="errors">
{section name=i loop=$errors}
{$errors[i]}<br />
{/section}
</div>
{/if}
{if!empty($errors)}
{section name=i loop=$errors}
{$errors[i]}
{/section}
{/if}
尽管调试时会让人困惑,但实际上这只是一个简单的全局变量$smarty
,它最初是在configs.php
中设置的smarty
对象,在包含test.php
时再次被覆盖。Smarty丢失了其配置的$template\u dir
,因为对象$Smarty
已重新初始化为默认值。模板的默认位置是查看/templates
,这就是为什么使用C:/xampp/htdocs/testing/templates/test.tpl
解决方案:不要做$smarty=newsmarty()
insidetest.php
test.php
string(55)“C:/xampp/htdocs/testing/templates/frontend/default/tpl\”}
在/tpl\”之后是否真的显示了一个尾随的反斜杠?不知道为什么var_dump($smarty->getTemplateDir());显示此项尝试强制它使用不带templateDir
的绝对路径,如$smarty->display('file:C:/xampp/htdocs/testing/templates/frontend/default/tpl/test.tpl')代码>(这正在运行C:/xampp/htdocs/testing/templates/test.tpl,但这不是我想要的。我想要C:/xampp/htdocs/testing/templates/frontend/default/tpl/t第三方物流
{if !empty($errors)}
<div id="errors">
{section name=i loop=$errors}
{$errors[i]}<br />
{/section}
</div>
{/if}
<?php
//$success = 'Success Message';
//$error = 'Error Message';
$errors[] = 'Error one';
$errors[] = 'Error two';
// Don't do this: $smarty is already initialized and configured.
// $smarty = new Smarty;
$smarty->assign('errors', $errors);
$smarty->display('test.tpl');
?>