ViewRenderer.php 2.42 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * Smarty view renderer class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

10
namespace yii\smarty;
11

Qiang Xue committed
12 13 14
use Yii;
use Smarty;
use yii\base\View;
15
use yii\helpers\Html;
16
use yii\base\ViewRenderer as BaseViewRenderer;
17 18 19 20 21 22 23

/**
 * SmartyViewRenderer allows you to use Smarty templates in views.
 *
 * @author Alexander Makarov <sam@rmcreative.ru>
 * @since 2.0
 */
24
class ViewRenderer extends BaseViewRenderer
25
{
26
	/**
Qiang Xue committed
27
	 * @var string the directory or path alias pointing to where Smarty cache will be stored.
28
	 */
29
	public $cachePath = '@runtime/Smarty/cache';
30 31

	/**
Qiang Xue committed
32
	 * @var string the directory or path alias pointing to where Smarty compiled templates will be stored.
33
	 */
34
	public $compilePath = '@runtime/Smarty/compile';
35 36

	/**
Qiang Xue committed
37
	 * @var Smarty
38
	 */
Qiang Xue committed
39
	public $smarty;
40 41 42

	public function init()
	{
Qiang Xue committed
43 44 45
		$this->smarty = new Smarty();
		$this->smarty->setCompileDir(Yii::getAlias($this->compilePath));
		$this->smarty->setCacheDir(Yii::getAlias($this->cachePath));
46

Alexander Makarov committed
47
		$this->smarty->registerPlugin('function', 'path', [$this, 'smarty_function_path']);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	}

	/**
	 * Smarty template function to get a path for using in links
	 *
	 * Usage is the following:
	 *
	 * {path route='blog/view' alias=$post.alias user=$user.id}
	 *
	 * where route is Yii route and the rest of parameters are passed as is.
	 *
	 * @param $params
	 * @param \Smarty_Internal_Template $template
	 *
	 * @return string
	 */
	public function smarty_function_path($params, \Smarty_Internal_Template $template)
	{
resurtm committed
66
		if (!isset($params['route'])) {
67 68 69 70 71 72 73
			trigger_error("path: missing 'route' parameter");
		}

		array_unshift($params, $params['route']) ;
		unset($params['route']);

		return Html::url($params);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	}

	/**
	 * Renders a view file.
	 *
	 * This method is invoked by [[View]] whenever it tries to render a view.
	 * Child classes must implement this method to render the given view file.
	 *
	 * @param View $view the view object used for rendering the file.
	 * @param string $file the view file.
	 * @param array $params the parameters to be passed to the view file.
	 *
	 * @return string the rendering result
	 */
	public function render($view, $file, $params)
	{
Qiang Xue committed
90 91
		/** @var \Smarty_Internal_Template $template */
		$template = $this->smarty->createTemplate($file, null, null, $params, true);
92 93 94 95

		$template->assign('app', \Yii::$app);
		$template->assign('this', $view);

Qiang Xue committed
96
		return $template->fetch();
97
	}
Zander Baldwin committed
98
}