@20171122读了 ecshop -index.php 代码,对其中主要的代码流程做了个总结,内容如下:
一、引入初始化文件 init.php(init.php 文件做什么,自己看去);
二、判断是否有缓存,没有缓存,执行 if 语句块中的代码;
if 语句块的代码,主要是做了各种模板数据赋值操作,例如:title,推荐商品,热点文章等等
if (!$smarty->is_cached('index.dwt', $cache_id))
{
assign_template();
....
assign_dynamic('index');
}
三、调用模板,$smarty 对象是 cls_template.php 类文件的实例化。
代码如下:
$smarty->display('index.dwt', $cache_id);
display() 方法是 $smarty 对象的方法。
display() 方法做了哪些事情呢?
首先是通过 $this->fetch() 方法,处理模板文件。
$this->fetch() 方法中,根据条件判断,调用了【处理字符串方法:$this->fetch_str()】 ,或者【编译模板函数方法: $this->make_compiled()】。
$this->fetch_str() 与 $this->make_compiled() 方法具体细节,自行了解。
处理完模板文件之后,对编辑处理完的模板文件字符串,进行 strpos 判断,是否存在对应的 $this->_echash。
如果为 true,就模板字符串进行 explode 拆分。拆分出来的格式如下:
Array
(
[0] => <div class="swiper-slide">
[1] => ads|a:3:{s:4:"name";s:3:"ads";s:2:"id";s:1:"1";s:3:"num";s:1:"1";}
[2] => </div>
)
foreach 遍历数组,将 $key 对 2 求模,结果为 1 的值,传送给 $this->inser_mod() 方法。
if (strpos($out, $this->_echash) !== false)
{
$k = explode($this->_echash, $out);
foreach ($k AS $key => $val)
{
if (($key % 2) == 1)
{
$k[$key] = $this->insert_mod($val);
}
}
$out = implode('', $k);
}
$this->insert_mod() 方法代码如下:
function insert_mod($name) // 处理动态内容
{
list($fun, $para) = explode('|', $name);
$para = unserialize($para);
$fun = 'insert_' . $fun;
return $fun($para);
}
网友评论