ViewRenderer.php 2.68 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\base\ViewRenderer as BaseViewRenderer;
16
use yii\helpers\Url;
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 27 28 29
    /**
     * @var string the directory or path alias pointing to where Smarty cache will be stored.
     */
    public $cachePath = '@runtime/Smarty/cache';
30

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

36 37 38 39
    /**
     * @var Smarty
     */
    public $smarty;
40

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

47 48
        $this->smarty->registerPlugin('function', 'path', [$this, 'smarty_function_path']);
    }
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    /**
     * 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)
    {
        if (!isset($params['route'])) {
            trigger_error("path: missing 'route' parameter");
        }
69

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

73 74
        return Url::to($params);
    }
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    /**
     * 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)
    {
        /** @var \Smarty_Internal_Template $template */
        $template = $this->smarty->createTemplate($file, null, null, empty($params) ? null : $params, true);
92

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

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