/** * 编译模板片段,并将模板片段放在name缓存文件中 * 模板文件中允许存在如下三种形式: * <pre> * 1、第一种: * <hook-action name="hook1" args='a,b,c'> * <div>i am from hook1 {$a} |{$b}|{$c}</div> * </hook-action> * 如上将会被编译成: * function templateName_hook1($a, $b, $c){ * } * 2、第二种: * <hook-action name="hook2"> * <div> i am from hook2 {$data} </div> * </hook-action> * 如上将会编译成: * function templateName_hook2($data){ * } * 3、第三种: * <div> i am from segment {$data}</div> * 如上将会被编译成: * function templateName($data) { * } * * 模板标签: * <segment alias='' name='' args='' tpl='' /> * tpl文件中的模板内容按照如上三种规则被编译之后,将会保存到__segment_alias文件中 * 调用方法根据: * tpl_name来调用,如果func没有写,则调用方法为tpl,否则为tpl_func,传入参数为args * </pre> * * @param string $template * @param array $args * @param string $func * @param string $alias * @param WindViewResolve $viewer * @return */ public static function segment($template, $args, $func = '', $alias = '', $viewer = null) { if ($viewer instanceof WindViewerResolver) { self::$viewer = $viewer; } $_prefix = str_replace(array(':', "."), '_', $template); $alias = '__segment_' . strtolower($alias ? $alias : $_prefix); list($templateFile, $cacheCompileFile) = self::$viewer->getWindView()->getViewTemplate($template); $pathinfo = pathinfo($cacheCompileFile); $cacheCompileFile = $pathinfo['dirname'] . '/' . $alias . '.' . $pathinfo['extension']; $_method = strtoupper($func ? $_prefix . '_' . $func : $_prefix); if (WIND_DEBUG) { WindFolder::mkRecur(dirname($cacheCompileFile)); WindFile::write($cacheCompileFile, '', WindFile::READWRITE); } else { if (!function_exists($_method) && is_file($cacheCompileFile)) { include $cacheCompileFile; } if (function_exists($_method)) { call_user_func_array($_method, $args); return; } } if (!($content = self::_resolveTemplate($templateFile, strtoupper($_prefix)))) { return; } $_content = array(); foreach ($content as $method => $_item) { $_tmpArgs = ''; foreach ($_item[1] as $_k) { $_tmpArgs .= '$' . trim($_k) . ','; } $windTemplate = Wind::getComponent('template'); $_content[] = '<?php if (!function_exists("' . $method . '")) {function ' . $method . '(' . trim($_tmpArgs, ',') . '){?>'; $_content[] = $windTemplate->compileStream($_item[0], self::$viewer); $_content[] = '<?php }}?>'; } WindFolder::mkRecur(dirname($cacheCompileFile)); WindFile::write($cacheCompileFile, implode("\r\n", $_content), WindFile::APPEND_WRITE); include $cacheCompileFile; call_user_func_array($_method, $args); }