在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>
...全文
1747 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" %}这个中前面的空格引起的呢
《php开发典型模块大全》读书笔记 第四章 smarty (张迅雷闪击PHP系列) 下载地址 http://www.smarty.net/download 测试使用的是Smarty-2.6.26\libs 公用的smarty类库和相关信息,将smarty放在服务器根目录下,配置信息写在一个文件,用的时候include下配置文件smarty的变量有以下三部分: a 来自php的变量,即assign()方法传过来的变量; 在tpl模板获取数组的值,两个方法 $arr[0] / 或者 $arr.name b 保留变量,相当于php的预定义变量 get post server Session Cookie request ;now;const;config {%* 注释 *%} 使用“#”,将变量名置于两个"#",就可以像普通变量一样调用配置文件内容了。 保留变量同时调用,在tpl文件加载配置文件 {% config_load file="04/04.conf" %} {% config_load file="04/04.conf" [section=section scope=local|parent|global] %} variable modifers 修饰变量 {% variable_name|modifer_name:parameter1:.... %} 内置函数: config_load foreach 使用foreach语句循环输出数组。 {% foreach key=key item=item from=$infobook %} {% $key %} => {% $item %} {% /foreach %} include {% include file='08/table.tpl' name='编程词典网' value='htt://www.mrbccd.com1' %} insert 用法与include相似,两者的区别是:使用insert函数载入的内容部会被缓存,每次开启面都会重新被执行。 {% if $smarty.get.type == 'mr' %} 欢迎光临,{% $smarty.get.type %} {% else %} 对不起,您不是本站VIP,无权访问此栏目。 {% /if %} section循环可以处理比较复杂的数组 {% section name=sec1 loop=$obj %} {% $obj[sec1].bigclass %} {% section name=sec2 loop=$obj[sec1].smallclass %}   {% $obj[sec1].smallclass[sec2].s_type %} {% /section %} {% /section %} void append() 向数组追加元素 void clear_all_assign() 清楚所有模板的赋值 void clear_assign(string var)清除乙肝制定的赋值 void config_load()加载配置文件 string fetch(string template) 返回模板的输出内容,但不直接显示出来; array get_config_vars()返回指定配置变量的值,如果没有参数,则返回一个所有配置变量的数组; array get_template_vars()返回指定模板变量的值,如果没有参数,则返回一个所有模板变量的数组; bool template_exists(string template) 检测制定的模板是否存在 缓存默认是3600秒,即1小时。 addslashes() 在操作数据库时,对其的特殊字符进行自动转义,即在特殊字符前加反斜杠,包括单引号‘ 双引号“ NULL,但是不包括% 和_ 另外还可以 mysql_real_escape_string()来进行转义,效果和addslashes()函数式一样的。

4,251

社区成员

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

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