在smarty莪在模板页tpl中调用配置文件conf、但网页总是解析不出来

KAIDXC 2011-04-09 04:40:37
这个小程序总共有三个文件、目录结构如下
/index.php
/config.php
/Smarty/templates/index.tpl
/Smarty/configs/04.conf

index.php

<?php
include_once 'config.php';
$smarty->display('index.tpl');
?>




config.php

<?php
header('Content-Type:text/html;charset=gb2312');
/* 定义服务器的绝对路径 */
define('BASE_PATH',$_SERVER['DOCUMENT_ROOT']);
/* 定义Smarty目录的绝对路径 */
define('SMARTY_PATH','/Smarty/');
/* 加载Smarty类库文件 */
require BASE_PATH.SMARTY_PATH.'Smarty.class.php';
/* 实例化一个Smarty对象 */
$smarty = new Smarty;
/* 定义各个目录的路径 */
$smarty->template_dir = BASE_PATH.SMARTY_PATH.'templates/';
$smarty->compile_dir = BASE_PATH.SMARTY_PATH.'templates_c/';
$smarty->config_dir = BASE_PATH.SMARTY_PATH.'configs/';
$smarty->cache_dir = BASE_PATH.SMARTY_PATH.'cache/';
$smarty->left_delimiter = '{%';
$smarty->right_delimiter = '%}';
$smarty->caching = false;
$smarty->debugging = false;
?>


04.conf

title = "调用配置文件"
bgcolor = "#000000"
border = "5"
type = "计算机类"
name = "PHP从入门到精通"


index.tpl

{% config_load file="04.conf" %}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{% #title# %}</title>
</head>
<body bgcolor="{% #bgcolor# %}">
<table border="{% #border# %}">
<tr>
<td>{% $smarty.config.type %}</td>
<td>{% $smarty.config.name %}</td>
</tr>
</table>
</body>
</html>




可是在chrome浏览器里打开会出错误、应该是无法识别到CONF文件的标记、返回结果如下、只有这些
{config_load file=04.conf}
{% $smarty.config.type %} {% $smarty.config.name %}
<body bgcolor="{% #bgcolor# %}">
<table border="{% #border# %}">
<tr>
<td>{% $smarty.config.type %}</td>
<td>{% $smarty.config.name %}</td>
</tr>
</table>
</body>
</html>
...全文
1748 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
feraner 2013-07-29
  • 打赏
  • 举报
回复
用过smarty ,但没出现过这种情况,不过有空格没空格要注意,

 include_once '../libs/Smarty.class.php';
   $smarty=new Smarty();
   $smarty->template_dir="../templates/";
   $smarty->compile_dir="../templates_c/";
   $smarty->left_delimiter="<{";
   $smarty->right_delimiter="}>";

<{$error3|default:" "}>//这里的空格有了就错
但是看了你的代码 貌似模板没有执行,直接按原字符输出了吧!以上给你一个参考 ,不知道能不能帮你
xuzuning 2013-07-21
  • 打赏
  • 举报
回复
你 {% config_load file="04.conf" %} 没有指定 04.conf 的路径,那么他应该和 smarty 的使用者 index.php 在同一目录中 config_load 的作用是加载与定义的,与项目相关的常量数据 按理也应该和使用者放在一起 你的形如 $smarty->template_dir = BASE_PATH.SMARTY_PATH.'templates/'; 这样的代码是蛇足的 项目的目录结构应该这样的: /index.php /config.php /templates /templates_c 至于 smarty 放在哪?无关紧要,总是在 config.php 中通过计算得到就行 无论有多少个项目,只需一个 smarty 就可以了
怪才 2013-07-21
  • 打赏
  • 举报
回复
应该是模板标记里的空格导致的 {%后的空格去掉 %}前的空格去掉 或者按照2#说的,把定义分隔符那里加上空格符
ohmygirl 2013-07-20
  • 打赏
  • 举报
回复
config中header('Content-Type:text/html;charset=gb2312'); 是干嘛的?
it观望者 2013-07-20
  • 打赏
  • 举报
回复
我也遇到这样的问题,真的很郁闷。
终小南 2011-04-13
  • 打赏
  • 举报
回复
smarty值需要你assign一下。。
lazyboy_wu 2011-04-09
  • 打赏
  • 举报
回复
你还不如干脆

$smarty->left_delimiter = '{% ';
$smarty->right_delimiter = ' %}';

注意有空格
standford 2011-04-09
  • 打赏
  • 举报
回复
是不是{% config_load file="04.conf" %}这个中前面的空格引起的呢

4,251

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧