> Zencart中文手册 > zen cart是怎么根据模版把内容显示出来的?

require('includes/application_top.php');初始化所有需要用到的公共信息以后,接下来就应该是显示了在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) {    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;    }}
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=product_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=index&cPath=48DIR_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/indexincludes/templates/template_default/indexincludes/templates/zccn/commonincludes/templates/template_default/common这四个目录下找html_header.php,在这里,最终在template_defaultcommon目录下找到html_header.php
到这里就可以自己写模板文件了,因为$template->get_template_dir()是按顺序找的,所以你只要在你的模板文件中存在该文件即可.
===================================================================
/includes/modules/pages/模块名/header_php.php (模块业务逻辑程序文件)
/includes/modules/pages/模块名/jscript_main.php (模块JS程序文件)
/includes/templates/模块名/templates/tpl_模块名_default.php (模块对应的模板文件)