php页面模板
‘壹’ 做个网站(php的),用许多的页面有相同的部分,如导航条之类的,想做个模板,问下怎么做,怎么加载
可以在php中先:
include($tpl->template('滑嫌唯moban.html'));
然后再再php中加载个Template.php 就可以了。
Template.php 代码如下:
<?php
class Template
{
public $templateDir = 'templates';
public $leftTag = '{';
public $rightTag = '}';
public $compileDir = 'cache';
public $compiledFileExt = '.TemplateCompiled.php';
public $templateFileExt = '.html'; //当display() cache() 不使用参数时使用
public $caching = false;
public $cacheDir = 'cache';
public $cacheDirLevels = 0; /者迹/信培缓存目录层次
public $cacheFileExt = '.TemplateCache.php';
public $cacheLifeTime = 3600; // 单位 秒
public $cacheID;
public $forceCompile = false;
public $lang=array();
private $cacheFile; //缓存文件,在_saveCache()中使用
private $realCacheID; //通过计算得出的缓存ID
const MAX_CACHE_DIR_LEVELS=16; //最大缓存目录层次数量
public function __construct($arrConfig = array())
{
foreach ($arrConfig as $key=>$val) {
$this->$key = $val;
}
if ($this->cacheDirLevels>self::MAX_CACHE_DIR_LEVELS) {
$this->cacheDirLevels=self::MAX_CACHE_DIR_LEVELS;
}
}
/**
* 判断缓存文件是否有效
*
* @param string $file
* @param string $cacheID
* @return boolean
*/
public function cached($file='',$cacheID='')
{
$file=$this->getTemplateFile($file);
$this->cacheID=$cacheID;
$cachefile=$this->getCacheFileName($file,$cacheID);
if ($this->caching && is_file($cachefile) && (filemtime($cachefile)+$this->cacheLifeTime)>time()) {
return true;
} else {
return false;
}
}
/**
* 返回模板文件完整路径
*
* @param string $file
* @return string
*/
private function getTemplateFile($file='')
{
if (!strlen($file)) {
$file=App::$controller.'_'.App::$action.$this->templateFileExt;
}
return $file;
}
/**
* 获取缓存文件完整路径
*
* @param string $file
* @param string $cacheID
* @return string
*/
private function getCacheFileName($file,$cacheID)
{
if (!strlen($this->realCacheID)) {
$this->realCacheID=$cacheID!=''?$cacheID:$_SERVER['SCRIPT_NAME'].$_SERVER['QUERY_STRING'];
$this->realCacheID.=$this->templateDir.$file.APP_NAME;
}
$md5id=md5($this->realCacheID);
$this->cacheDirLevel=$this->getCacheDirLevel($md5id);
return $this->cacheDir.$this->cacheDirLevel.'/'.$md5id.$this->cacheFileExt;
}
/**
* 获取缓存目录层次
*
*/
private function getCacheDirLevel($md5id)
{
$levels=array();
$levelLen=2;
for ($i=0; $i<$this->cacheDirLevels; $i++) {
$levels[]='TepmlateCache_'.substr($md5id,$i*$levelLen,$levelLen);
}
return !count($levels) ? '' : '/'.implode('/',$levels);
}
/**
* 在$this->compile()中替换$foo.var为数组格式$foo['var']
*
*/
private function compile_replace($str)
{
$str=preg_replace('/(\$[a-z_]\w*)\.([\w]+)/',"\\1['\\2']",$str);
return $this->leftTag.$str.$this->rightTag;
}
/**
* 编译模板文件
*
* @param string $file
* @return string
*/
private function compile($file='')
{
$file=$this->getTemplateFile($file);
$fullTplPath=$this->templateDir.'/'.$file;
$compiledFile=$this->compileDir.'/'.md5($fullTplPath).$this->compiledFileExt;
if ($this->forceCompile || !is_file($compiledFile) || filemtime($compiledFile)<=filemtime($fullTplPath)) {
$content=file_get_contents($fullTplPath);
$leftTag=preg_quote($this->leftTag);
$rightTag=preg_quote($this->rightTag);
$search=array(
'/'.$leftTag.'include ([\w\.\/-]+)'.$rightTag.'/i', //导入子模板
'/'.$leftTag.'(\$[a-z_]\w*)\.(\w+)'.$rightTag.'/i', //将模板标签{$foo.var}修改为数组格式{$foo['var']}
'/'.$leftTag.'(.+?\$[a-z_]\w*\.\w+.*?)'.$rightTag.'/ie', //将模板标签中的$foo.var修改为数组格式$foo['var']
'/'.$leftTag.'(else if|elseif) (.*?)'.$rightTag.'/i',
'/'.$leftTag.'for (.*?)'.$rightTag.'/i',
'/'.$leftTag.'while (.*?)'.$rightTag.'/i',
'/'.$leftTag.'(loop|foreach) (.*?) as (.*?)'.$rightTag.'/i',
'/'.$leftTag.'if (.*?)'.$rightTag.'/i',
'/'.$leftTag.'else'.$rightTag.'/i',
'/'.$leftTag."(eval) (.*?)".$rightTag.'/is',
'/'.$leftTag.'\/(if|for|loop|foreach|while)'.$rightTag.'/i',
'/'.$leftTag.'((( *(\+\+|--) *)*?(([_a-zA-Z][\w]*\(.*?\))|\$((\w+)((\[|\()(\'|")?\$*\w*(\'|")?(\)|\]))*((->)?\$?(\w*)(\((\'|")?(.*?)(\'|")?\)|))){0,})( *\.?[^ \.]*? *)*?){1,})'.$rightTag.'/i',
'/'.$leftTag.'\%([\w]+)'.$rightTag.'/', //多语言
);
$replace=array(
'<?php include($tpl->template("\\1"));?>',
$this->leftTag."\\1['\\2']".$this->rightTag,
"\$this->compile_replace('\\1')",
'<?php }else if (\\2){ ?>',
'<?php for (\\1) { ?>',
'<?php $__i=0; while (\\1) {$__i++; ?>',
'<?php $__i=0; foreach ((array)\\2 as \\3) { $__i++; ?>',
'<?php if (\\1){ ?>',
'<?php }else{ ?>',
'<?php \\2; ?>',
'<?php } ?>',
'<?php echo \\1;?>',
'<?php echo $this->lang["\\1"];?>',
);
$content=preg_replace($search,$replace,$content);
file_put_contents($compiledFile,$content,LOCK_EX);
}
return $compiledFile;
}
/**
* 根据是否使用缓存,输出缓存文件内容
*
* @param string $tplFile
* @param string $cacheID
*/
public function cache($tplFile,$cacheID='')
{
$this->cacheID=$cacheID;
$cacheFile=$this->getCacheFileName($file,$cacheID);
if ($this->cached($file,$cacheID)) {
readfile($cacheFile);
exit;
} elseif ($this->caching) {
ob_start(array(&$this,'_saveCache'));
$this->cacheFile=$cacheFile;
}
}
/**
* 返回编译后的模板文件完整路径
*
* @param string $file
* @return string
*/
public function template($file='')
{
$file=$this->getTemplateFile($file);
return $this->compile($file);
}
/**
* 回调函数,供cache()函数使用
*
* @param string $output
* @return string
*/
public function _saveCache($output)
{
$cacheDir=$this->cacheDir.$this->cacheDirLevel;
is_dir($cacheDir) or mkdir($cacheDir,0777,true);
file_put_contents($this->cacheFile,$output,LOCK_EX);
return $output;
}
}//end class
‘贰’ PHP模板是什么
平常我们做一个交互式网站,一定会关注两个主要的问题,就是美工和程序。这也是一个网站在建设中抛开其内容之后最关键的要素。
通常有两种方式来协调美工和程序之间的关系:
1.先做好美工页面,然后由程序员直接在美工页面的Html文件中嵌入ASP、jsp、PHP等程序代码。
2.美工和程序同时进行,但这时因为没有页面框架,程序只能做出一些关键代码,双方完成后再进行一次美工页面和程序代码的嵌入合成。
在实际的网站建设过程中,由于人员、进度等环境的限制,大家通常会混合地使用上面两种协调方式。然而这两种方法都有不足之处:
1. 效率不高。两者协调不好可能产生等待、重复代码调试步骤等现象;
2. 调试不畅。由于程序代码最终需要嵌入在HTML页面中,代码的嵌入、调试、纠错都比较繁琐;
3. 维护不便。一旦美工设计需要修改,如网站改版,那么所有程序和HTML代码混合页面都需要重写;
如果你正在使用PHP程序建设网站,那么PHP的模板技术会比较圆满地解决上述问题。
那么什么是PHP的模板技术?PHP模板即PHPlib的Template技术,是PHPLIB程序库中的一个主要模块之一,发展自Perl的Template。而PHPLIB则是在PHP上的一个扩展,提供了很多类库,能够方便地实现一些基本功能如用户认证,数据库封装等。
现在PHP模板技术很多,不过建议你学习smarty,毕竟是官方的东西!
smarty学习很简单的,你去下载一个smarty手册,对照着学,基本上二天就可以学会了!
‘叁’ 在php的模板页面怎么做判断,下面是我的详细介绍
如果用PHP判断,直接在添加的单选按钮上写
<inputtype="radio"<?phpif(condition){echo'checked="checked"';}?>value="".../>
如果是JS判断,则可以尝试这样
if(condition){
document.getElementById('id').setAttribute("checked","checked");
}
JQ就更简单一些
if(condition){
$([selector]).attr("checked","checked");
}
condition是判断条件,selector是选择器,有些地方省略写了,注意修改。
‘肆’ 用PHP制作静态网站的模板框架(二)
PHP代码全部保存到单独的文件中,这个文件也就是由页面URL实际调用的文件。Web服务器通过PHP引擎解析该文件,然后把结果返回给浏览器。一般地,PHP代码总是动态地生成页面内容,比如查询数据库或者执行某种计算等。下面是一个例子:
<?php
//
example.php
require('class.FastTemplate.php');
$tpl
=
new
FastTemplate('.');
$tpl->define(
array(
'main'
=>
'main.htm',
'header'
=>
'header.htm',
'leftnav'
=>
'leftnav.htm'
)
);
//
此处的PHP代码设置$content使其包含合适的页面内容
$tpl->assign('CONTENT',
$content);
$tpl->parse('HEADER',
'header');
$tpl->parse('LEFTNAV',
'leftnav');
$tpl->parse('MAIN',
'main');
$tpl->FastPrint('MAIN');
?>
这里我们使用的是流行的FastTemplate模板类,但其基本思路对于其他许多模板类来说都一样。首先你实例化一个类,告诉它到哪里去寻找模板文件以及哪一个模板文件与页面的哪部分对应;接下来是生成页面内容,把结果赋予内容的标识符;然后,依次解析各个模板文件,模板类将执行必要的替换操作;最后把解析结果输出到浏览器。
这个文件完全由PHP代码构成,不包含任何HTML代码,这是它最大的优点。现在,PHP程序员可以集中精力编写生成页面内容的代码,而不必为了如何生成HTML去正确地格式化最终页面而担心。
你可以使用这种方法和上面的文件构造出一个完整的网站。如果PHP代码是以URL中的查询字符串为基础生成页面内容,例如http://www.foo.com/example.php?article=099,你可以据此构造出一个完整的杂志网站。
很容易看出采用模板还有第二个好处。如上例所示,页面左边的导航条单独保存为一个文件,我们只需编辑这一个模板文件就可以改变网站所有页面左边的导航条。
‘伍’ php程序的网站,模版在哪里
问题比较笼统呀
你先说清楚你用的什么框架,ThinkPhp?
应该是了,用的比较多。MVC模式的
模型就是moudle了,去Model文件夹找;
控制器就是controller,也就是存入类文件的;
视图就是View,就是页面;