昨天装上smarty,可是在配置模板的路径时遇到问题。
define("ROOT", str_replace("\\", "/", dirname(__FILE__)).'/');
require 'libs/Smarty.class.php';
$smarty= new Smarty();
$smarty->setTemplateDir(ROOT.'templates/');
print_r($smarty->getTemplateDir()); 运行输出的模板路径如下:
F:/PHP program/Smarty/templates\
也就是最后的那个路径分隔符怎么也调不对,在、一直没调好,怎么办?SmartyPHP
define("ROOT", str_replace("\\", "/", dirname(__FILE__)).'/');
require 'libs/Smarty.class.php';
$smarty= new Smarty();
$smarty->setTemplateDir(ROOT.'templates/');
print_r($smarty->getTemplateDir()); 运行输出的模板路径如下:
F:/PHP program/Smarty/templates\
也就是最后的那个路径分隔符怎么也调不对,在、一直没调好,怎么办?SmartyPHP
$smarty->setTemplateDir('templates');相对路径就可以了
模板总是跟随应用的
最后还是有个分隔符,不知道哪来的...
如果没有路径符,就不好拼装地址了
比如 模板目录中有一个 admin 子目录,现在要用其中的 test.tpl 模板
就可写作
$smarty->diplay('admin/test.tpl');
实际使用的就是 templates/admin/test.tpl
如果 $smarty->getTemplateDir() 没有最后的路径符,就得写作
$smarty->diplay('/admin/test.tpl');
这就会产生歧义了
Uncaught exception 'SmartyException' with message 'Unable to load template file 'demo.htm'' in F:\PHP program\Smarty\libs\sysplugins\smarty_internal_templatebase.php:127 Stack trace: #0 F:\PHP program\Smarty\libs\sysplugins\smarty_internal_templatebase.php(362): Smarty_Internal_TemplateBase->fetch('demo.htm', NULL, NULL, NULL, true) #1 F:\PHP program\Smarty\demo.php(5): Smarty_Internal_TemplateBase->display('demo.htm') #2 {main} thrown in F:\PHP program\Smarty\libs\sysplugins\smarty_internal_templatebase.php on line 127
+- Smarty.class.php
templates
+- demo.htm
你的程序require 'libs/Smarty.class.php';
$smarty = new Smarty();
$smarty->display('demo.htm');
include 'init.inc.php';
$smarty->assign("title","测试用的标题");
$smarty->assign("content","测试用内容");
$smarty->display("demo.htm");
?>
我郁闷的是为什么输出的模板路径最后一个分隔符与前面的分隔符是反着的,而且改不过来
linux 下路径符为 /
window 下默认路径符为 \ 同时支持 /
<?php
include 'init.inc.php';
$smarty->assign("title","测试用的标题");
$smarty->assign("content","测试用内容");
$smarty->display("demo.htm");
// print_r($smarty->getTemplateDir());
?>
init.inc.php:
<?php
require 'libs/Smarty.class.php';
$smarty= new Smarty();
$smarty->setTemplateDir('templates');
//$smarty->left_delimiter='<{';
//$smarty->right_delimiter='}>';
print_r($smarty->getTemplateDir());
?>
demo.htm:
<html>
<head>
<meta http-equiv="Content-type" content="text/html"; charset="utf-8">
<title><{title}></title>
</head>
<body>
<{$content}>
</body>
</html>
执行demo.php输出:
Array ( [0] => templates\ )
Fatal error: Uncaught exception 'SmartyException' with message 'Unable to load template file 'demo.htm'' in F:\PHP program\Smarty\libs\sysplugins\smarty_internal_templatebase.php:127 Stack trace: #0 F:\PHP program\Smarty\libs\sysplugins\smarty_internal_templatebase.php(362): Smarty_Internal_TemplateBase->fetch('demo.htm', NULL, NULL, NULL, true) #1 F:\PHP program\Smarty\demo.php(5): Smarty_Internal_TemplateBase->display('demo.htm') #2 {main} thrown in F:\PHP program\Smarty\libs\sysplugins\smarty_internal_templatebase.php on line 127
不知道怎么改。