在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>
...全文
1742 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()函数式一样的。
graduation 毕业设计代码,还没完成!但可以跑哦~ Fis3+Es6+Smarty+Tmpl 项目实战 阅读以下部分假定你已经了解: 构建工具 Fis3 基于Smarty的解决方案 Fis-Smarty 一些杂碎 Less, Tmpl, Es6 ... 微薄网站的设计与实现 功能 展示,留言,评论,评论量,访问量 后台CMS 瀑布流展示,懒加载数据 接入微博组件等 技术方案 前后端分离 构建: Fis3 结构: HTML,TMPL,Smarty 样式: Less 类库/框架: jQuery util: urlParam ... 数据: 假数据模拟,后期发布开发机联调 目录结构 前端目录 └─graduation ├─ README.md ├─ home └─ common ├─ plugin # Smarty 的插件放入这个目录 ├─ smarty.conf # 本地测试的 Smarty 引擎的配置文件 ├─ fis-conf.js # fis3 的配置文件 ├─ page # 放一些面 ├─ server.conf # 本地测试的URL转发规则配置文件 ├─ static # 非组件静态资源 ├─ test # 假数据 └─ widget # 放一些 widget,里面代码最终会被组件化封装 ├─ header # 头部模块 │ ├─ header.js # js文件es6 │ ├─ header.less # css预处理 │ ├─ header.tmpl # JS模板 │ └─ header.tpl # Smarty 复用还是挺方便的 ├─ nav # 导航模块 ├─ article # 文章模块 └─ footer # 尾部模块 目录文件不是都必须需要,一般都会包含page、widget俩目录 前端预览或发布到本地模拟开发机预览该项目 发布到本地模拟开发机预览该项目 修改 fis-global-conf.js const RECEIVER = 'http://127.0.0.1/graduation-rd/receiver.php'; const to_path = '/graduation-rd'; const base_url = '/Applications/XAMPP/xamppfiles/htdocs/graduation-rd/'; 安装 npm install -g fis3 npm install -g fis3-smarty 后端环境下载 启动 fis3 release -r common xuecheng fis3 release -r home xuecheng fis3 release -r ... 发布成功后目录 └─ htdocs └─graduation-rd ├── application ├── home.php ├── license.txt ├── receiver.php ├── system ├── user_guide ├── template # .tpl 发布位置 ├── static # 静态资源发布位置 ├── plugin # plugin/*.php 发布位置 └── config # config/*.json 发布位置

4,251

社区成员

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

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