关于php的一个小疑问

来源:百度知道 编辑:UC知道 时间:2024/07/02 16:57:03
最近看了些网上的php源码,大家好像都有个templates目录,里面放着一些htm文件,里面这些文件是做什么的?
另外,为什么访问的php文件,比如默认首页文件index.php,里面全是php语言,没有网站样式的语言?但是这是怎么回事?

templates一般是存放着模版文件,就是网站的样式、结构、布局等。

类似index.php都有一个index.html文件与之对应,index.php通过程序,读取模板文件,并通过语言把指定内容代替为变量值,或循环输出内容。

这样做可以实现把脚本和模版分开,程序员专职写程序,美工人员专职界面美工,互不影响。

这是smarty,将php代码和html代码分开,你可以看到templates中的html文件中有些一用{}包起来的标签,smarty通过将这些标签转换成PHP页中以经定义的PHP代码或变量,再次运行,得到最终的结果,
你还可以看到templates_c,cache,等文件夹,templates_c是用来存放smarty以经解释了每一次的文件,如果开始cache,会在cache中存放页面最终的页面,这是为了提高smarty的速度。
例子
<?php
//require smarty 并指定文件夹对应的路径
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = 'smarty/templates/';
$smarty->compile_dir = 'smarty/templates_c/';
$smarty->config_dir = 'smarty/configs/';
$smarty->cache_dir = 'smarty/cache/';
$smarty->assign('name','Ned');//定义了$name变量为"Ned"
$smarty->display('index.html');//显示index.html
?>

index.html页中只需要
<html>
<body>
Hello,