From 34c0794f0097588d50a032818964b027751ce038 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 14 Apr 2013 22:50:35 -0400 Subject: [PATCH] script WIP --- framework/base/View.php | 9 +-- framework/base/ViewContent.php | 176 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 173 insertions(+), 12 deletions(-) diff --git a/framework/base/View.php b/framework/base/View.php index d1a3c5f..247dfd2 100644 --- a/framework/base/View.php +++ b/framework/base/View.php @@ -37,7 +37,7 @@ class View extends Component /** * @var ViewContent */ - public $content; + public $page; /** * @var mixed custom parameters that are shared among view templates. */ @@ -88,10 +88,10 @@ class View extends Component if (is_array($this->theme)) { $this->theme = Yii::createObject($this->theme); } - if (is_array($this->content)) { - $this->content = Yii::createObject($this->content); + if (is_array($this->page)) { + $this->page = Yii::createObject($this->page); } else { - $this->content = new ViewContent; + $this->page = new ViewContent; } } @@ -166,7 +166,6 @@ class View extends Component } else { $output = $this->renderPhpFile($viewFile, $params); } - $output = $this->content->populate($output); $this->afterRender($viewFile, $output); } diff --git a/framework/base/ViewContent.php b/framework/base/ViewContent.php index cea3c7c..9bdeaef 100644 --- a/framework/base/ViewContent.php +++ b/framework/base/ViewContent.php @@ -8,6 +8,7 @@ namespace yii\base; use Yii; +use yii\helpers\Html; /** * @author Qiang Xue @@ -19,6 +20,10 @@ class ViewContent extends Component const POS_BEGIN = 2; const POS_END = 3; + const TOKEN_HEAD = ''; + const TOKEN_BODY_BEGIN = ''; + const TOKEN_BODY_END = ''; + /** * @var array * @@ -44,7 +49,7 @@ class ViewContent extends Component * ) * ~~~ */ - public $bundles; + public $assetBundles; public $title; public $metaTags; public $linkTags; @@ -57,11 +62,6 @@ class ViewContent extends Component public $jsInBody; public $jsFilesInBody; - public function populate($content) - { - return $content; - } - public function reset() { $this->title = null; @@ -77,7 +77,169 @@ class ViewContent extends Component $this->jsFilesInBody = null; } - public function renderScripts($pos) + public function begin() + { + ob_start(); + ob_implicit_flush(false); + } + + public function end() + { + $content = ob_get_clean(); + echo $this->populate($content); + } + + public function beginBody() + { + echo self::TOKEN_BODY_BEGIN; + } + + public function endBody() + { + echo self::TOKEN_BODY_END; + } + + public function head() + { + echo self::TOKEN_HEAD; + } + + public function requireAssetBundle($name) + { + if (!isset($this->assetBundles[$name])) { + $bundle = Yii::$app->assets->getBundle($name); + if ($bundle !== null) { + $this->assetBundles[$name] = $bundle; + } else { + throw new InvalidConfigException("Unknown asset bundle: $name"); + } + foreach ($bundle->depends as $d) { + $this->requireAssetBundle($d); + } + } + } + + public function registerMetaTag($options, $key = null) + { + if ($key === null) { + $this->metaTags[] = Html::tag('meta', '', $options); + } else { + $this->metaTags[$key] = Html::tag('meta', '', $options); + } + } + + public function registerLinkTag($options, $key = null) + { + if ($key === null) { + $this->linkTags[] = Html::tag('link', '', $options); + } else { + $this->linkTags[$key] = Html::tag('link', '', $options); + } + } + + public function registerCss($css, $options = array(), $key = null) + { + $key = $key ?: $css; + $this->css[$key] = Html::style($css, $options); + } + + public function registerCssFile($url, $options = array(), $key = null) + { + $key = $key ?: $url; + $this->cssFiles[$key] = Html::cssFile($url, $options); + } + + public function registerJs($js, $position = self::POS_END, $options = array(), $key = null) + { + $key = $key ?: $js; + $html = Html::script($js, $options); + if ($position == self::POS_END) { + $this->js[$key] = $html; + } elseif ($position == self::POS_HEAD) { + $this->jsInHead[$key] = $html; + } elseif ($position == self::POS_BEGIN) { + $this->jsInBody[$key] = $html; + } else { + throw new InvalidParamException("Unknown position: $position"); + } + } + + public function registerJsFile($url, $position = self::POS_END, $options = array(), $key = null) + { + $key = $key ?: $url; + $html = Html::jsFile($url, $options); + if ($position == self::POS_END) { + $this->jsFiles[$key] = $html; + } elseif ($position == self::POS_HEAD) { + $this->jsFilesInHead[$key] = $html; + } elseif ($position == self::POS_BEGIN) { + $this->jsFilesInBody[$key] = $html; + } else { + throw new InvalidParamException("Unknown position: $position"); + } + } + + + protected function populate($content) + { + $this->expandAssetBundles(); + return strtr($content, array( + self::TOKEN_HEAD => $this->getHeadHtml(), + self::TOKEN_BODY_BEGIN => $this->getBodyBeginHtml(), + self::TOKEN_BODY_END => $this->getBodyEndHtml(), + )); + } + + protected function expandAssetBundles() + { + + } + + protected function getHeadHtml() + { + $lines = array(); + if (!empty($this->metaTags)) { + $lines[] = implode("\n", $this->cssFiles); + } + if (!empty($this->linkTags)) { + $lines[] = implode("\n", $this->cssFiles); + } + if (!empty($this->cssFiles)) { + $lines[] = implode("\n", $this->cssFiles); + } + if (!empty($this->css)) { + $lines[] = implode("\n", $this->css); + } + if (!empty($this->jsFilesInHead)) { + $lines[] = implode("\n", $this->jsFilesInHead); + } + if (!empty($this->jsInHead)) { + $lines[] = implode("\n", $this->jsInHead); + } + return implode("\n", $lines); + } + + protected function getBodyBeginHtml() + { + $lines = array(); + if (!empty($this->jsFilesInBody)) { + $lines[] = implode("\n", $this->jsFilesInBody); + } + if (!empty($this->jsInHead)) { + $lines[] = implode("\n", $this->jsInBody); + } + return implode("\n", $lines); + } + + protected function getBodyEndHtml() { + $lines = array(); + if (!empty($this->jsFiles)) { + $lines[] = implode("\n", $this->jsFiles); + } + if (!empty($this->js)) { + $lines[] = implode("\n", $this->js); + } + return implode("\n", $lines); } } \ No newline at end of file