smarty中组合的模板的应用,找了半天资料也没搞好 `请帮忙`

jekom 2009-01-24 03:25:22
index.tpl
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" c>
<title>TITLE</title>
<link href="header.css" rel="stylesheet" type="text/css">
</head>
<body>
{include_php file="../header.php"}
{include file="header.tpl"}
</body>
</html>


index.php
require('../libs/Smarty.class.php');
$smarty = new Smarty;

$smarty->template_dir = 'templates';
$smarty->compile_dir = 'templates_c';
$smarty->config_dir = 'configs';
$smarty->cache_dir = 'cache';
$smarty->display("index.tpl");


header.tpl
{$display}


header.php
$display="正文";
$smarty->assign("display",$display);

=========================================================================
我这样用为什么不能正常显示?显示不出来``请高人帮下我``说详细点行吗
...全文
435 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
phnessu4 2009-01-31
  • 打赏
  • 举报
回复
既然用来template为什么还要向里面嵌入php文件呢?
-----------------------------------------------
同问来着,但刚才google了下,找到了一个解决方案,同样也是答案

简单的来说lz的include顺序有问题.
lz原来的顺序是

index.php

└─index.tpl

├─header.php
└─header.tpl

应该是

index.php

└─index.tpl

└─header.php

└─header.tpl


应该在header.php中加入
$smarty->display(header.tpl)

这样当需要修改header.php或tpl 的时候不会牵涉到index的东西,我想这就是lz想要的效果吧..

参阅资料
froole兄15楼所言
http://www.sherryping.cn/Mervin.G/?action=show&id=37&page=1
http://lizi.blogbus.com/logs/10069282.html
TR@SOE 2009-01-31
  • 打赏
  • 举报
回复
Smarty的模板嵌套只能包含入别的模板,下面是我目前的一个实现:

index.tpl:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>任氏有无轩</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="任氏有无轩" name="keywords" />
<meta content="任氏有无轩" name="description" />
<link href="../style.css" type="text/css" rel="stylesheet" />
</head>
<body>

{include file="top.tpl" curPage="index"}<!--调用顶部导航条文件-->

<!--三大栏目块-->
{include file="bottom.tpl"}<!--开始调用底部文件-->

</body>
</html>


top.tpl:
<!--调用顶部LOGO栏文件-->
{include file='logo.tpl'}

<!--菜单导航条-->

<div class="menudiv" id="menu">
<div class="float_left width_a">
{if $curPage == "index"}
<a href="/books/index.php" class="menua-cur" title="首 页">首 页</a>
{else}
<a href="/books/index.php" class="menua" title="首 页">首 页</a>
{/if}
</div>
...


index.php:
    require ("../init.php");
require_once ("functions.php");

$sm->assign("thisyear", date("Y"));
$sm->assign("summary", getSummary());

$row1=getNewBuyBooks();
$row2=getNewReadBooks();
$row3=getMisc();

$sm->assign("row1", $row1);
$sm->assign("row2", $row2);
$sm->assign("row3", $row3);
$sm->display("book_index.tpl");
jekom 2009-01-31
  • 打赏
  • 举报
回复
19楼的兄弟正解,谢谢了,但是你有个地方错了
==================================
应该在header.php中加入
$smarty->display(header.tpl) =>改成 $this->display(header.tpl)

这样就正常运行了`

解决问题谢谢大家,人人有分,新年快乐!
phnessu4 2009-01-31
  • 打赏
  • 举报
回复
刚写了个小节,关于smarty 文件嵌套调用,lz看看便知
http://ph4r.wordpress.com.cn/2009/01/31/smarty-%e6%96%87%e4%bb%b6%e5%b5%8c%e5%a5%97%e8%b0%83%e7%94%a8/
「已注销」 2009-01-30
  • 打赏
  • 举报
回复
不太对头....{include_php file="../header.php"} 逻辑关系比较是问题....
最好是把php代码和tpl模板分开~~~
jekom 2009-01-30
  • 打赏
  • 举报
回复
呵呵``楼上的解决方法,是正确的,我当时也是这样处理的

===================================================

menu.tpl可以自动从session或者global中提取内容,
当然,menu.tpl也可以从index.tpl所对应的php中取得内容,
但是这种结合共同处理内容的处理需要清晰的构架,又是另一个概念。

其实,我1直在想,既然选择了模板,为什么不能单独的模块去单独的去处理,而要选择这样从session或者global中提取 或者 这种结合共同处理内容的处理,那这样1来,smarty只能做到HTML和PHP分离,这样并不能来解决我们进行快速开发(需要什么功能就调用什么模块),那smarty的优势并没体现出来.

或者你会告诉我用框架,但是在中,小型项目中用框架确实是没意义的,所以我才想到用smarty来做,这样smarty能实现吗?
jekom 2009-01-29
  • 打赏
  • 举报
回复
我知道Smarty将index.tpl编译成一个PHP文件,我怀疑是运行错误,路径是没问题的`

=============================================================================
既然用来template为什么还要向里面嵌入php文件呢?

因为有时候网页的头部文件,比如菜单做成include就比较方便修改,这样就需要把头部文件include到index页面中,菜单又是动态的,所以就需要inlude php文件读取数据.

大家平时都没遇到这样的问题吗?
海诗美妆 2009-01-29
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 froole 的回复:]
引用 10 楼 jekom 的回复:
我知道Smarty将index.tpl编译成一个PHP文件,我怀疑是运行错误,路径是没问题的`

=============================================================================
既然用来template为什么还要向里面嵌入php文件呢?

因为有时候网页的头部文件,比如菜单做成include就比较方便修改,这样就需要把头部文件include到index页面中,菜单又是动态的,所以就需要inlude php文件读取数据.

大家平时都没遇…
[/Quote]

lz还不太明白smarty的用法吧,多看看官方文档
海诗美妆 2009-01-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jekom 的回复:]
我知道Smarty将index.tpl编译成一个PHP文件,我怀疑是运行错误,路径是没问题的`

=============================================================================
既然用来template为什么还要向里面嵌入php文件呢?

因为有时候网页的头部文件,比如菜单做成include就比较方便修改,这样就需要把头部文件include到index页面中,菜单又是动态的,所以就需要inlude php文件读取数据.

大家平时都没遇到这样的问题吗?
[/Quote]
模板之间也可以包含的,一样很方便。
yctin 2009-01-29
  • 打赏
  • 举报
回复
header.php
$display="正文";
$smarty->assign("display",$display);
这里那来的$smarty object 啊,已经变成数组了



require('../libs/Smarty.class.php');
$smarty = new Smarty;

$smarty->template_dir = 'templates';
$smarty->compile_dir = 'templates_c';
$smarty->config_dir = 'configs';
$smarty->cache_dir = 'cache';
include('header.php');//加在这里才对吧
$smarty->display("index.tpl");

海诗美妆 2009-01-29
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 jekom 的回复:]
比如:菜单的摸板meun.html,当然就有个meun.php(从数据库里动态读出菜单)的文件 display(meun.html);
那我在index.html里 <{include file="meun.html"}>,这样虽然meun.html包含进去了``但是这样只是静态的meun.html,而我需要是动态的,楼上的能详细说下吗?谢谢了
[/Quote]

貌似lz并不明白Smarty模板、PHP之间的关系。

假设你的菜单是通过menu.tpl模板显示,
你把menu.tpl里面需要现实的内容放到一个可以共享的区域,例如session或者global
index.tpl里面包含menu.tpl,
显示menu.tpl的时候不需要去调用menu.php,直接调用index.tpl所对应的php。

menu.tpl可以自动从session或者global中提取内容,
当然,menu.tpl也可以从index.tpl所对应的php中取得内容,
但是这种结合共同处理内容的处理需要清晰的构架,又是另一个概念。

不知道是否可以理解。


一个tpl不等于一个php,更不等于一个页面——
一个页面可以用n个tpl,一个php可以对应n个tpl,在一个页面的tpl可以用n个tpl组合。
jekom 2009-01-29
  • 打赏
  • 举报
回复
比如:菜单的摸板meun.html,当然就有个meun.php(从数据库里动态读出菜单)的文件 display(meun.html);
那我在index.html里<{include file="meun.html"}>,这样虽然meun.html包含进去了``但是这样只是静态的meun.html,而我需要是动态的,楼上的能详细说下吗?谢谢了
jekom 2009-01-29
  • 打赏
  • 举报
回复
模板之间也可以包含的, 这个知道是{inlude file="摸板文件"}这样吗?我先去官网看看``楼上的能详细说下吗?
balpha 2009-01-28
  • 打赏
  • 举报
回复
$smarty->display('index.tpl')我感觉这样的解决很简单!
海诗美妆 2009-01-28
  • 打赏
  • 举报
回复
应该是你的include_path没有设定好

把../libs的绝对路径添加到include_path里面
胡争辉 2009-01-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 felixrong 的回复:]
既然用来template为什么还要向里面嵌入php文件呢?

[/Quote]
也许是遗留代码
felixrong 2009-01-26
  • 打赏
  • 举报
回复
既然用来template为什么还要向里面嵌入php文件呢?
胡争辉 2009-01-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jekom 的回复:]
index.tpl有没有编译,不知道怎么判断编译错还是运行错,帮忙看下哦 ``所有代码都在上面``你可以试试
[/Quote]
你知道Smarty将index.tpl编译成一个PHP文件不?
jekom 2009-01-24
  • 打赏
  • 举报
回复
index.tpl有没有编译,不知道怎么判断编译错还是运行错,帮忙看下哦 ``所有代码都在上面``你可以试试
胡争辉 2009-01-24
  • 打赏
  • 举报
回复
看index.tpl有没有编译,区分是编译错还是运行错。
加载更多回复(2)

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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