zencart函数解析

1、寻找文件的函数
function zen_get_file_directory($check_directory, $check_file, $dir_only = 'false') {
     global $template_dir; //为了访问函数体外定义的$template_dir变量
    $zv_filename = $check_file;
    if (!strstr($zv_filename, '.php')) $zv_filename .= '.php';
    if (file_exists($check_directory . $template_dir . '/' . $zv_filename)) {
      $zv_directory = $check_directory . $template_dir . '/';
    } else {
      $zv_directory = $check_directory;
    }
    if ($dir_only == 'true') {
      return $zv_directory;
    } else {
      return $zv_directory . $zv_filename;
    }
  }

该函数用于在指定的目录下,寻找模板(作为全局参数存在)下的指定文件。如果文件不存在,则返回默认文件,即直接在指定目录下的文件。如果最后一个参数传入的是true,说明需要的是dir,则返回最接近指定文件的那个目录。
如 调用:zen_get_file_directory('includes/languages/schinese/html_includes/','define_main_page',false);
假设当前的模板是classic。
首先会给文件名(第二个参数),补充一个.php后缀,然后在includes/languages/schinese/html_includes/classic目录下,寻找是否存在define_main_page.php的文件,如果存在,那么最接近的目录就是includes/languages/schinese/html_includes/classic了,这时如果要返回目录(第三个参数为true)则会把includes/languages/schinese/html_includes/classic返回;如果要返回的是文件,则会返回includes/languages/schinese/html_includes/classic/define_main_mage.php这个文件。
如果在includes/languages/schinese/html_includes/classic不存在指定的文件,则最近的目录将会变为:
includes/languages/schinese/html_includes/,如果要返回文件,则会返回includes/languages/schinese/html_includes/define_main_page.php(不管存不存在)。

2、获取modules下面的指定文件或文件所在的目录
function zen_get_module_directory($check_file, $dir_only = 'false') {
    global $template_dir;  //使用外部变量
    $zv_filename = $check_file;
    if (!strstr($zv_filename, '.php')) $zv_filename .= '.php';  //文件文件自动加上.php后缀
    if (file_exists(DIR_WS_MODULES . $template_dir . '/' . $zv_filename)) {  //如果modules下的当前模板文件夹下存在指定的文件,则认为结果目录为当前模板,否则就是空,表示当前模板下不存在指定的文件。
      $template_dir_select = $template_dir . '/';
    } else {
      $template_dir_select = '';
    }
    if ($dir_only == 'true') {
      return $template_dir_select;
    } else {
      return $template_dir_select . $zv_filename; //返回文件
    }
  }
如外部调用:require(DIR_WS_MODULES . zen_get_module_directory('require_languages.php'));
其中DIR_WS_MODULES 为includes/modules
假设当前的模板为classic,如果includes/modules/classic下不存在require_languages.php,则会返回
includes/modules/下的require_languages.php,如果这个地方也没有,那就报错了,因为用的是require。

3、

function get_template_part($page_directory, $template_part, $file_extension = '.php') {
    $directory_array = array();
    if ($dir = @dir($page_directory)) {
      while ($file = $dir->read()) {
        if (!is_dir($page_directory . $file)) {
          if (substr($file, strrpos($file, '.')) == $file_extension && preg_match($template_part, $file)) {
            $directory_array[] = $file;
          }
        }
      }
      sort($directory_array);
      $dir->close();
    }
    return $directory_array;
  }
表示在指定的$page_directory下,寻找后缀名为$file_extension指定的,且名称符合$template_part正则要求的文件(不是目录),找到就将其加入数组,最后一起返回。
$code_page_directory="includes/modules/pages/about_future/"
$directory_array = $template->get_template_part($code_page_directory, '/^header_php/');
这个意思,就是在"includes/modules/pages/about_future/"目录下,寻找以'head_php'开头的php文件。

4、get_template_dir($template_code, $current_template, $current_page, $template_dir, $debug=false)
这个函数就是体现了zencart的模板选择的灵活性:
$template_code就是目标文件,如html_header.php
$current_template就是当前所选择的模板所在的目录,如includes/templates/classic
$current_page就是当前main_page参数指定的页面,如about_future
$template_dir就是备选模板目录,如common。

整个查找过程是这样的:先在模板目录下看有没有要的文件,有就返回那个目录;如果没有,则看默认模板template_default下有没有这个文件,有就返回那个目录;如果没有,到回模板目录的子目录$template_dir下,看有没有这个文件,有就返回那个目录;如果没有,则直接返回默认模板template_default目录的$template_dir子目录。当然这时候如果还没有,则就不行了,报错。而template_default/common下总是有的。

代码为:
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, preg_replace('/\//', '', $template_code), $debug)) {
      return DIR_WS_TEMPLATES . 'template_default/' . $current_page;
    } elseif ($this->file_exists($current_template . $template_dir, preg_replace('/\//', '', $template_code), $debug)) {
      return $current_template . $template_dir;
    } else {
      return DIR_WS_TEMPLATES . 'template_default/' . $template_dir;
      // return $current_template . $template_dir;
    }
  }

举个例子:
DIR_WS_TEMPLATE="includes/templates/classic/";
$current_page_base="classic";
$template->get_template_dir('html_header.php',DIR_WS_TEMPLATE, $current_page_base,'common')
查找过程为:
到includes/templates/classic/下看有没有html_header.php;
没有就到includes/templates/template_default/下看有没有;
没有就到includes/templates/classic/common下看有没有;
没有就直接返回includes/templates/template_default/common这个目录。

顺便说一下,这个html_header.php的作用是输出最终文件的<head></head>之间的内容,从它的命名也可以看出。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值