public function partial($file, $data = null, $options = null)
{
if ($data instanceof Pix_Partial) {
$data = $data->_data;
} else {
$data = (array) $data;
}
if (array_key_exists('_data', $data)) {
$data = array_shift($data);
}
if ($this->_path) {
$path = $this->_path . ltrim($file, '/');
} else {
$path = $file;
}
if (!is_null($options) and is_scalar($options)) {
// $options 是 scalar 的話, 就是 cache id
$cache_id = $options;
} elseif (is_array($options)) {
// 是 array 的話, 就從 array 取出設定
$cache_id = $options['cache_id'];
$cache = $options['cache'];
}
if (!$cache instanceof Pix_Cache) {
$cache = new Pix_Cache();
}
$cache_key = sprintf('Pix_Partial:%s:%s:%s:%s:%s:%s', $this->_cache_prefix, strtolower($_SERVER['HTTP_HOST']), sha1(file_get_contents($path)), $cache_id, self::$_trim_mode ? 1 : 0, self::$_minify_mode ? 1 : 0);
if (!self::$_nocache and !self::$_write_only_mode and strlen($cache_id) > 0 and $html = $cache->load($cache_key)) {
return $html;
}
// TODO: 這邊要改漂亮一點的寫法
$old_data = $this->_data;
$this->_data = $data;
ob_start();
try {
if (!file_exists($path)) {
throw new Exception("找不到 {$path} 這個路徑的 partial");
}
if (preg_match('#.tmpl$#', $path)) {
$this->jQueryTmpl($path, $this);
} else {
require $path;
}
$str = ob_get_clean();
} catch (Pix_Partial_NoRender $e) {
ob_get_clean();
$str = '';
} catch (Pix_Partial_BreakRender $e) {
$str = ob_get_clean();
} catch (Exception $e) {
ob_get_clean();
throw $e;
}
if (self::getCommentMode() and $str) {
$str = "<!-- Pix_Partial START {$file} -->\n{$str}\n<!-- Pix_Partial END {$file} -->\n";
}
$this->_data = $old_data;
if (self::$_trim_mode) {
$newstr = '';
foreach (explode("\n", $str) as $line) {
$line = trim($line);
if (!empty($line)) {
$newstr .= $line . "\n";
}
}
$str = trim($newstr);
}
if (self::$_minify_mode) {
$str = Minify_HTML::minify($str, ['cssMinifier' => ['Minify_CSS', 'minify'], 'jsMinifier' => ['JSMin', 'minify']]);
}
if (!self::$_nocache and strlen($cache_id) > 0) {
$cache->save($cache_key, $str);
}
return $str;
}