zencart加载流程

分析最外层的index.php,这个是控制器。
总体加载流程为:
* <ul>
 * <li>Load application_top.php - see {@tutorial initsystem}</li>
 * <li>Set main language directory based on $_SESSION['language']</li>
 * <li>Load all *header_php.php files from includes/modules/pages/PAGE_NAME/</li>
 * <li>Load html_header.php (this is a common template file)</li>
 * <li>Load main_template_vars.php (this is a common template file)</li>
 * <li>Load on_load scripts (page based and site wide)</li>
 * <li>Load tpl_main_page.php (this is a common template file)</li>
 * <li>Load application_bottom.php</li>
 * </ul>

1、首先加载application_top.php
require('includes/application_top.php');
这个文件做了很多事情,如参数检查,自动加载各个init_includes和class下的大量文件,page目录获取等等;凡是后面用到的变量不在configure.php设定的,都是在这里得到的;
如请求的main_page=about_future,language为english,当前的模板为classic,那么:
DIR_WS_LANGUAGES ='includes/languages/english';
$code_page_directory='includes/modules/pages/about_future/';
$current_page_base='about_future';
DIR_WS_TEMPLATE='includes/templates/classic';
具体可以看关于init_sanitize.php的作用解析。


2、加载main_page参数指定的includes/modules/pages/$_GET['main_page'] 目录下的各个head_php开头的php文件;
$language_page_directory = DIR_WS_LANGUAGES . $_SESSION['language'] . '/';
  $directory_array = $template->get_template_part($code_page_directory, '/^header_php/');
  foreach ($directory_array as $value) {
 * We now load header code for a given page.
 * Page code is stored in includes/modules/pages/PAGE_NAME/directory
 * 'header_php.php' files in that directory are loaded now.
 */
    require($code_page_directory . '/' . $value);
  }


3、加载模板下的html_header.php文件。
 require($template->get_template_dir('html_header.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/html_header.php');
这个用法让我明白了,原来还可在模板目录下新建立一个以main_page的值(如about_future)命名的文件夹,里面放置html_header.php来设置about_future将得到的<head></head>里的东西。就像在pages下建立一个about_future子目录一样。不同的是,pages下的是必须要建立的,而templates下的是可选的。没有这个html_header.php的话,就都用template_default/common下的html_header.php。
下面的main_template_vars.php文件也是如此。


4、加载变量定义文件:main_template_vars.php
require($template->get_template_dir('main_template_vars.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/main_template_vars.php');

这个main_template_vars.php干了什么呢?打开templates/template_default/common下的该文件查看:
 if (file_exists(DIR_WS_MODULES . 'pages/' . $current_page_base . '/main_template_vars.php')) {
    $body_code = DIR_WS_MODULES . 'pages/' . $current_page_base . '/main_template_vars.php';
  } else {
    $body_code = $template->get_template_dir('tpl_' . preg_replace('/.php/', '',$_GET['main_page']) . '_default.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). ' /tpl_' . $_GET['main_page'] . '_default.php';
  }

红色就是我们在模板文件里定义的页面对应的模板文件,但是其优先级是较低,可以被modules/pages/[main_page]下的main_template_vars.php覆盖。什么时候会需要这样做呢?
* Normally a page will automatically load its own template based on the page name.<br />
 * so that a page called some_page will load tpl_some_page_default.php from the template directory.<br />
 * <br />
 * However sometimes a page may need to choose the template it displays based on a set of criteria.<br />
 * Placing a file in the includes/modules/pages/some_page/ directory called main_template_vars.php<br />
 * allows you to override this page and choose the template that loads.<br />
也就是说,在这个main_template_vars.php写代码根据条件来加载不同的tpl文件。

这里的 $body_code就是后面的tpl_main_page.php里要使用的用于输出中间的部分。



5、准备加载相应的on_load开头的js文件:
$directory_array = $template->get_template_part(DIR_WS_MODULES . 'pages/' . $current_page_base, '/^on_load_/', '.js');
foreach ($directory_array as $value) {
    $onload_file = DIR_WS_MODULES . 'pages/' . $current_page_base . '/' . $value;
    $read_contents='';
    $lines = @file($onload_file);
    foreach($lines as $line) {
      $read_contents .= $line;
    }
  $za_onload_array[] = $read_contents;
  }
对这个数组进行适当的处理,最终得到一个字符串:如includes/..../on_load_a.js;includes/.../on_load_b.js;
用”;“好分隔的。到时候到了tpl文件,直接输出就可以了。



6、经过一系列的处理,终于到了tpl文件了:
require($template->get_template_dir('tpl_main_page.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/tpl_main_page.php');

注意,这个tpl_main_page.php并不是我们在templates/classic/templates/tpl_about_future_default.php文件,而是一个结构文件, Normally consisting of a header, left side column. center column. right side column and footer。
中间的内容才是我们的tpl_about_future_default.php文件!
自然,清楚了它的结构以后,我们就可以改造这个结构文件了!
如果我希望main_page=about_future这个页面不要那么复杂,什么顶部logo,左栏右栏,都不要,那么就可以在
includes/templates/classic(或再下一级/common里建立,看起寻找的规律)下建立一个tpl_main_page.php文件,里面自由发挥,也是可以的!
关于这个tpl_main_page.php的作用的解析在另外一篇介绍。

7、最后一步是加载application_bottom.php文件,
require(DIR_WS_INCLUDES . 'application_bottom.php');
该文件就是关闭session。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值