建议用smarty或者smarttemplate!呵呵<?php include("Smarty.class.php"); $smarty = new Smarty; $smarty->caching = true; // only do db calls if cache doesn't exist if(!$smarty->is_cached("index.tpl")) { // dummy up some data $address = "245 N 50th"; $db_data = array( "City" => "Lincoln", "State" => "Nebraska", "Zip" => "68502" ); $smarty->assign("Name","Fred"); $smarty->assign("Address",$address); $smarty->assign($db_data); } // capture the output $output = $smarty->fetch("index.tpl"); // do something with $output here echo $output; ?>
<?php require_once("b.php");?>
<html>
<head>
<title><?php show_title();?></title>
</head>
<body>
<?php show_body();?>
</body>
</html>
b.php
<?php
function show_title()
{
echo "hello!word!";
}function show_body()
{
echo "here is body contents!";
}?>这个方法的好处就是执行效率要比我的前一种方法快!!而且不用使用TAG!!怎么样,大家帮我分析下,如果没有缺陷的话以后我就这么做了!
可以将A.PHP 和 B.PHP进行合并成一个文件,将B.PHP的代码放在A.PHP的页首,这样减少一次读入的过程,同时也减少了文件数量,不用一个模板一个程序了,同时也便于以后的更新维护!但是如果是团体合作的化还是要分成两个文件,因为要各自开发。不知道那些所谓的“模板技术”效果是否能超越我的这种做法??
(快3点了,想了一夜,我倒!)
<?php require_once("b.php");?>
<html>
<head>
<title><?php show_title();?></title>
</head>
<body>
<?php show_body();?>
</body>
</html>这个不仍旧是 html 和 php 结合在一起 ? 只不过原本是 php 包含 html, 现在改成了 html 里面包含 php.这个模板乍一看似乎没什么不好, 但潜在问题有很多.1. 每次显示 php 内容的时候要调用 <?php 某函数() ?> 这样一来你必须使用 ob_start() 来控制输出, 否则频繁的 echo 会导致页面渲染速度飞降, 长达 1, 2 秒也有可能.2. 你给出的例子代码中只提到了 show_title(); 这种简单文本输出, 如果要输出一个大表格, 表格有 100 个单元, 你不可能用上述方法来逐个显示表格单元吧 ? 你只能制定一个函数输出表格整体, 这样你的 php 代码里面又不可避免要有 "<table>", "<tr>", "<td>" 的 html
Discuz!、phpwind就是这么做的,并没有什么不爽。至少他们还在不断的推出新版本。“后来又在网上找到各种模板技术,什么PHPLIB等等,感觉用起来有些不爽。我想到以前生成静态HTML时,读入一个HTML模板,然后替换相应内容生成HTML页,用这个方法来实现HTML与PHP的分离不是更好吗?先读入一个HTML模板,然后使用REPLACE替换掉相应设置好的TAG,然后ECHO这个替换后的字符串变量到浏览器,这样多简单啊??”
是的是很简单,PHPLIB就是这么做的。你不是“感觉用起来有些不爽”吗?至于编译型的模板引擎smarty及smartTemplate等,你根本就没有接触。现在就加以评论还为时过早吧?!
alert('ok');
</script>
news.php
<?php
include "smarty.class.php";
$smarty = new smarty();
$sql = "select * from news limit0,10";
$news = $DB->getRows($sql); //DB类返回数组
$smarty->assige("news",$news);//替换模版中的news
$smarty->display("news.tpl.htm");//加载模板
?>news.tpl.htm
<html>
<li>标题</li>
{section name=i loop=$news}//需替换的内容 循环数组$news
<li>{$news[i].title}</li>//显示数组内容 --标题
{/section}//结束循环
</html>
假设数据结构有 字段title--标题
你的第一贴毛好毛好
<html>
...
<body>
$abc
</body>
</html>
程序a.php
<?php
//一系列操作后$abc="一些要显示的数据";
require_once("a.html");
?>这样做比楼主的想法好啊
不过还是推荐 用smarty
<?php
print<<< HTML
-->
<html>
...
<body>
$abc
</body>
</html>
<!--
HTML;
?>
-->
真知灼见,学习ing
include("Smarty.class.php");
$smarty = new Smarty; $smarty->caching = true; // only do db calls if cache doesn't exist
if(!$smarty->is_cached("index.tpl")) { // dummy up some data
$address = "245 N 50th";
$db_data = array(
"City" => "Lincoln",
"State" => "Nebraska",
"Zip" => "68502"
); $smarty->assign("Name","Fred");
$smarty->assign("Address",$address);
$smarty->assign($db_data); } // capture the output
$output = $smarty->fetch("index.tpl"); // do something with $output here echo $output;
?>
万一你真的觉得用别人的模板类不爽,就自己写吧,把要指望开始就把全部写出来,慢慢来吧~啊门!
用php做到狗一样累
{ function Page()
{
$this->CONF = $GLOBALS['CONF'];
$this->rD = $this->CONF['root_dir'];
$this->rP = $this->CONF['root_path'];
$this->pP = $this->CONF['pic_path'];
$this->uri = $_SERVER['REQUEST_URI'];
session_set_cookie_params(time()+123456789);
session_start();
} function set($title, $h1, $lPageFile,
$mWidth = NULL, $rWidth = NULL,
$rPageFile = NULL, $mPageFile = NULL,
$headType = 1, $footType = 1, $css='douban001.css')
{
/*
init
*/
$this->t = $title;
$this->h1 = $h1;
$this->lPF = $lPageFile;
$this->mW = $mWidth;
$this->rW = $rWidth;
$this->mPF = $mPageFile;
$this->rPF = $rPageFile;
$this->hT = $headType;
$this->fT = $footType;
$this->css = $this->CONF['css_path'].$css; function mkPage()
{
$this->mkH();
$this->mkNav();
$this->mkH1();
$this->mkL();
if (!is_null($this->mW)) $this->mkM();
if (!is_null($this->rW)) $this->mkR();
$this->mkF();
}
function get()
{
return $this->p;
}
function mkL()
{
$this->p .= '<td id="ml" valign="top">'."\n";
if (!is_null($this->lPF)) require_once($this->lPF);
$this->p .= '</td>'."\n";
}
function mkM()
{
$this->p .= '<td id="mm" width="'.$this->mW.'">';
echo $this->mPF;
if (!is_null($this->mPF)) require_once($this->mPF);
$this->p .= '</td>'."\n";
}
function mkR()
{
$this->p .= '<td id="mr" valign="top" width="'.$this->rW.'">'."\n";
if (!is_null($this->rPF)) require_once($this->rPF);
$this->p .= '</td>'."\n";
}
}
index.php里面<?php
//FINISHED
require_once('includes/config.php');
require_once($CONF['class_dir'].'class_page.php');$page = new Page();
$page->set('title显示什么','这里是一个h1的标题', 'index_l.php', 60, 350, 'index_r.php');
echo $page->get();index_l.php和index_r.php分别是include进来的两个页面,里面只有一个heredoc方式的赋值(赋给$this->p)
?>
纯HTML+PHP分离法(就是PHP不替换Template中架构)
类似Smarty Template方法(就是PHP不替换Template中架构)打一个比方: 一下两个文件
genvar.php
$data['var'] = 'a';teplate.php
<table>
<tr>
<td>
<?php echo $data['var'];
if ($data['var'] == 'a'){
?>
</td>
<?php } ?>
</tr>
</table>
以上就是一般的效率最高的无PHP对HTML架构执行对比+替换的方式
我喜欢用这个
利用这个让你一生都不要使用echo了....以下的Template是这样的
supertemplate.tpl
<td>{tdinfo}</td>
然后将有一个专门的PHP文件将其中的{tdinfo}替换为其他的东西,这个一般速度较慢,但是加入if then 之类的简单logic之后更容易看懂
没有必要重发发明轮子,这就是开源的好处
“没有必要重发发明轮子,这就是开源的好处”我最讨厌的就是听到这种话,轮子要造,要多造,造了也不一定要非用。