ViewContent.php 5.25 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\helpers\Html;
Qiang Xue committed
12

Qiang Xue committed
13 14 15 16 17 18 19 20 21 22
/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ViewContent extends Component
{
	const POS_HEAD = 1;
	const POS_BEGIN = 2;
	const POS_END = 3;

Qiang Xue committed
23 24 25 26
	const TOKEN_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>';
	const TOKEN_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>';
	const TOKEN_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>';

Qiang Xue committed
27
	/**
Qiang Xue committed
28
	 * @var \yii\web\AssetManager
Qiang Xue committed
29
	 */
Qiang Xue committed
30
	public $assetManager;
Qiang Xue committed
31

Qiang Xue committed
32
	public $assetBundles;
Qiang Xue committed
33 34 35 36 37
	public $title;
	public $metaTags;
	public $linkTags;
	public $css;
	public $cssFiles;
Qiang Xue committed
38
	public $js;
Qiang Xue committed
39
	public $jsFiles;
Qiang Xue committed
40 41 42 43
	public $jsInHead;
	public $jsFilesInHead;
	public $jsInBody;
	public $jsFilesInBody;
Qiang Xue committed
44

Qiang Xue committed
45 46 47 48
	public function init()
	{
		parent::init();
		if ($this->assetManager === null) {
Qiang Xue committed
49
			$this->assetManager = Yii::$app->getAssets();
Qiang Xue committed
50 51 52
		}
	}

Qiang Xue committed
53 54 55 56 57 58 59
	public function reset()
	{
		$this->title = null;
		$this->metaTags = null;
		$this->linkTags = null;
		$this->css = null;
		$this->cssFiles = null;
Qiang Xue committed
60
		$this->js = null;
Qiang Xue committed
61
		$this->jsFiles = null;
Qiang Xue committed
62 63 64 65
		$this->jsInHead = null;
		$this->jsFilesInHead = null;
		$this->jsInBody = null;
		$this->jsFilesInBody = null;
Qiang Xue committed
66 67
	}

Qiang Xue committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	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;
	}

Qiang Xue committed
95
	public function registerAssetBundle($name)
Qiang Xue committed
96 97
	{
		if (!isset($this->assetBundles[$name])) {
Qiang Xue committed
98
			$bundle = $this->assetManager->getBundle($name);
Qiang Xue committed
99
			if ($bundle !== null) {
Qiang Xue committed
100
				$this->assetBundles[$name] = false;
Qiang Xue committed
101
				$bundle->registerAssets($this);
Qiang Xue committed
102
				$this->assetBundles[$name] = true;
Qiang Xue committed
103 104 105
			} else {
				throw new InvalidConfigException("Unknown asset bundle: $name");
			}
Qiang Xue committed
106 107
		} elseif ($this->assetBundles[$name] === false) {
			throw new InvalidConfigException("A cyclic dependency is detected for bundle '$name'.");
Qiang Xue committed
108 109
		}
	}
Qiang Xue committed
110

Qiang Xue committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	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);
	}

Qiang Xue committed
141
	public function registerJs($js, $options = array(), $key = null)
Qiang Xue committed
142
	{
Qiang Xue committed
143 144
		$position = isset($options['position']) ? $options['position'] : self::POS_END;
		unset($options['position']);
Qiang Xue committed
145 146 147 148 149 150 151 152 153 154 155 156 157
		$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");
		}
	}

Qiang Xue committed
158
	public function registerJsFile($url, $options = array(), $key = null)
Qiang Xue committed
159
	{
Qiang Xue committed
160 161
		$position = isset($options['position']) ? $options['position'] : self::POS_END;
		unset($options['position']);
Qiang Xue committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		$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)
	{
		return strtr($content, array(
			self::TOKEN_HEAD => $this->getHeadHtml(),
			self::TOKEN_BODY_BEGIN => $this->getBodyBeginHtml(),
			self::TOKEN_BODY_END => $this->getBodyEndHtml(),
		));
	}

	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()
Qiang Xue committed
221
	{
Qiang Xue committed
222 223 224 225 226 227 228 229
		$lines = array();
		if (!empty($this->jsFiles)) {
			$lines[] = implode("\n", $this->jsFiles);
		}
		if (!empty($this->js)) {
			$lines[] = implode("\n", $this->js);
		}
		return implode("\n", $lines);
Qiang Xue committed
230
	}
Qiang Xue committed
231
}