您的位置:首页 > 其它

zencart主要文件的功能总结和提炼

2010-06-13 12:59 302 查看
zencart的源码文件中第一句话往往是包含include目录下的application_top.php文件,如:require(’includes/application_top.php’); 在zencart系统中application_top.php负责的是初始化工作,比如加载配置文件include(’includes/configure.php’);,如果系统没检测到该文件的存在则会尝试调用安装文件。然后它会自动遍历include/extra_configures下的配置文件并包含进来。 在加载了系统配置文件以后接下来是一个非常重要的文件,这也导致了zencart和oscommerce感觉上很大不同的原因,首先调用一个文件require(’includes/initsystem.php’); 在 initsystem.php中最先加载include/auto_loaders/config.core.php,config.core.php是一个二围数组$autoLoadConfig,即以数组的形式保存文件的信息供褐矜文件调用,然后系统会接着加载完 include/auto_loaders目录下所有文件名匹配$loaderPrefix(默认为config)的文件。 上面程序执行完以后就是加载自动执行程序了require(’includes/autoload_func.php’);在这里它会遍历$autoLoadConfig数组,它最终执行的效果会包含所有必须用到的函数或者类的定义,还有变量的初始化,config.core.php里面的注释比较清楚比如 $autoLoadConfig[0][] = array(’autoType’=>’class’'loadFile’=>’class.base.php’); 在autoload_func.php里面执行完以后的效果就是require(DIR_WS_CLASSES . ‘class.base.php’),大部分的初始化化工作是通过包含init_includes目录下的文件来实现的,如: $autoLoadConfig[110][] = array(’autoType’=>’init_script’'loadFile’=> ‘init_templates.php’); 它在执行完autoload_func.php文件后就已经加载了init_includes目录下的init_templates.php文件。 下面来介绍下zencart是怎么根据摸版把内容显示出来的。 在index.php的第29行有句 $directory_array = $template->get_template_part($code_page_directory ‘/^header_php/’); 由于所有初始化工作已经完成,所以我们就可以在上面的文件找督他们的定义如 $autoLoadConfig[100][] = array(’autoType’=>’classInstantiate’'className’=>’template_func’'objectName’=>’template’); 在这里就定义了$template = new template_func(); 然后$code_page_directory变量的定义是在init_includes/init_sanitize.php文件中定义在这里必须要对class/template_func.php中定义的template_func类比较熟悉,在该类中主要定义了两个方法 get_template_dir()和get_template_part(); 这两个方法在zencart的模板使用中起到了决定性的作用。 get_template_dir 方法function get_template_dir($template_code $current_template $current_page $template_dir $debug=false)它定义了5个参数,第一个参数一般是个文件名它是用来判断后两个参数组成的目录中有没有匹配$template_code 的这个文件该类复写了默认的系统函数file_exists所以很多初学者可能会比较迷惑 function get_template_dir($template_code $current_template $current_page $template_dir $debug=false) { //echo ‘template_default/’ . $template_dir . ‘=’ . $template_code; if($this->file_exists($current_template . $current_page $template_code)){ return $current_template . $current_page . ‘/’; }elseif ($this->file_exists(DIR_WS_TEMPLATES . ‘template_default/’ . $current_page ereg_replace(’/' ” $template_code) $debug)){ return DIR_WS_TEMPLATES . ‘template_default/’ . $current_page; } elseif ($this->file_exists($current_template . $template_dir ereg_replace(’/' ” $template_code) $debug)){ return $current_template . $template_dir; } else { return DIR_WS_TEMPLATES . ‘template_default/’ . $template_dir; //return $current_template . $template_dir; } } /* 如 includes/templates/zccn/index includes/templates/template_default/index includes/templates/zccn/common includes/templates/template_default/common */ get_template_part()方法有两个函数,第一个参数是文件目录,第二个参数是匹配的条件,执行的结果是包含该目录下所有文件名匹配这个条件的文件 比如$directory_array = $template->get_template_part($code_page_directory ‘/^header_php/’); 这句话执行的结果就是返回目录下$code_page_directory所有文件名以header_php开头的文件 如此时的url(http://localhost/zencart/index.php?main_page=p roduct_info&cPath=49_27&products_id=83) 你现在应该查看init_sanitize.php中$code_page_directory的定义此时的$code_page_directory的值应该是includes/modules/product_info/ 所以它就应该包含该目录下所有以header_php开头的文件在这里好象就只有一个header_php.php $directory_array = $template->get_template_part($code_page_directory ‘/^header_php/’);这个包含文件其实是初始化前台不同页面显示所需要用到的变量函数,主要是初始化数据库的东西因为每个页面需要的数据资料都有可能不同所以index.php?main_page=index 当main_page的值不同是在includes/modules/目录下都会有个对应的目录,这里是index目录 只要知道了这两个方法的用法,你就会知道模板文件都是怎么显示出来的了 再来解释一 require($template->get_template_dir(’html_header.php’DIR_WS_TEMPLATE $current_page_base’common’). ‘/html_header.php’); 假设当前url:http://localhost/zencart/index.php?main_page=i ndex&cPath=48 DIR_WS_TEMPLATE 定义是在includes/init_templates.php中定义define(’DIR_WS_TEMPLATE’ DIR_WS_TEMPLATES . $template_dir . ‘/’);因为我现在用的是默认的zccn模板 所以现在的DIR_WS_TEMPLATE=includes/templates/zccn/ $current_page_base在这里已经就是index 上面已经解释了$template->get_template_dir()的方法了 程序会依次在 includes/templates/zccn/index includes/templates/template_default/index includes/templates/zccn/common includes/templates/template_default/common 这四个目录下找html_header.php,在这里,最终在template_default\common目录下找督html_header.php 到这里就可以自己写摸板文件了因为$template->get_template_dir()是按顺序找赌,所以你只要在你的模板文件中存在该文件即可

原文:http://www.shunyi.net/forum/view/id-3670
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: