为什么智能模板引擎找不到我的模板文件?
致命错误:未捕获 - > Smarty:无法加载模板文件 'test.tpl'< - 抛入 C:\ XAMPP \ htdocs中\测试\包括\ smarty的\ sysplugins \ smarty_internal_templatebase.php 第129行
也做了$smarty->testInstall();
Smarty Installation test...
Testing template directory...
C:\xampp\htdocs\testing\templates\frontend\default\tpl is OK.
Testing compile directory...
C:\xampp\htdocs\testing\templates_c\frontend is OK.
Testing plugins directory...
C:\xampp\htdocs\testing\includes\smarty\plugins is OK.
Testing cache directory...
C:\xampp\htdocs\testing\cache is OK.
Testing configs directory...
C:\xampp\htdocs\testing\configs is OK.
Testing sysplugin files...
... OK
Testing plugin files...
... OK
Tests complete.
var_dump($smarty->getTemplateDir());
array(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}
答案 0 :(得分:1)
令人困惑的是调试它,实际上只是简单的全局变量$smarty
,它是Smarty
最初在configs.php
中设置的对象,再次被覆盖当test.php
被包括在内时。 Smarty丢失了配置的$template_dir
,因为对象$smarty
已重新初始化为默认值。模板的默认位置是查看./templates
,这就是使用C:/xampp/htdocs/testing/templates/test.tpl
的原因。
解决方案:不要在$smarty = new Smarty();
test.php
进行操作
<?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');
?>