我的文件结构:
--header.php
--smarty
--templates
-- x.tpl
--cache
--configs
--templates_c
--articles
-- testPage.phpheader.php中的代码
$smarty = new Smarty();
$smarty->setTemplateDir('smarty/templates');
$smarty->setCompileDir('smarty/templates_c');
$smarty->setCacheDir('smarty/cache');
$smarty->setConfigDir('smarty/configs');testPage.php中的代码
<?php
include('../header.php');
$smarty->display('x.tpl');
?>我遇到了这个错误:
PHP Fatal error: Uncaught exception 'SmartyException' with message 'Unable to
load template file 'x.tpl'' in
/usr/local/lib/php/Smarty/sysplugins/smarty_internal_templatebase.php:127如何在testPage.php中设置访问smarty模板的正确路径?
发布于 2012-12-16 20:56:36
简而言之,由于您需要从testpage.php向上转到包含smarty目录的目录,就像您对header.php include所做的那样,因此您需要对smarty include目录执行相同的操作。
$smarty->setTemplateDir('../smarty/templates');一种很好的方法是定义如何到达项目的根目录,然后在include中使用它。
例如在testPage.php中
define("PATH_TO_ROOT", "../");然后在header.php中
$smarty->setTemplateDir(PATH_TO_ROOT.'smarty/templates');
$smarty->setCompileDir(PATH_TO_ROOT.'smarty/templates_c');
$smarty->setCacheDir(PATH_TO_ROOT.'smarty/cache');
$smarty->setConfigDir(PATH_TO_ROOT.'smarty/configs');这使得从可能位于另一个位置的另一个PHP文件设置Smarty目录变得非常简单。例如,在名为“PATH_TO_ROOT /webtests/frontend”的目录中,您可以将测试定义为“../”,调用setup Smarty仍然可以工作。
您还可以让header.php检查是否定义了PATH_TO_ROOT,以防止直接调用它。
顺便说一句,您可能希望考虑不将templates_c和缓存目录放在Smarty目录下,而是在其他地方创建一个单独的目录来写入生成的数据(因此可能容易受到注入攻击)。对于我的项目,我有一个位于项目根目录之外的“var”目录,其中包含日志文件、缓存、生成的模板等的所有目录。“var”子目录中的所有内容都被认为是“不安全的”,这使得思考什么是安全的,什么不是很容易。
https://stackoverflow.com/questions/13900483
复制相似问题