Pix_Partial::partial PHP Method

partial() public method

partial 印出 $file 的內容,並且將 $data 的參數轉成 $this
public partial ( string $file, array | Pix_Partial | null $data = null, array | string $options = null ) : string
$file string
$data array | Pix_Partial | null 傳進去的參數
$options array | string 設定 或 cache id
return string
    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;
    }

Usage Example

コード例 #1
0
ファイル: Alert.php プロジェクト: yslbc/twcompany
 /**
  * alert popup javascript alert with $message and redirect to $url
  *
  * @param Pix_Controller $controller
  * @param string $message
  * @param string $url
  * @access public
  * @return void
  */
 public function alert($controller, $message, $url)
 {
     $view = new Pix_Partial(__DIR__ . '/Alert/');
     echo $view->partial('alert.phtml', array('message' => $message, 'url' => $url));
     return $controller->noview();
 }