mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Smarty: added path function and exposed $app and $this
This commit is contained in:
		@ -65,3 +65,18 @@ or `$this->renderPartial()` from your controller:
 | 
				
			|||||||
```php
 | 
					```php
 | 
				
			||||||
echo $this->render('renderer.tpl', array('username' => 'Alex'));
 | 
					echo $this->render('renderer.tpl', array('username' => 'Alex'));
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Additional functions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Additionally to regular Smarty syntax the following is available in Yii:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```php
 | 
				
			||||||
 | 
					<a href="{path route='blog/view' alias=$post.alias}">{$post.title}</a>
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					path function calls `Html::url()` internally.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Additional variables
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- `$app` = `\Yii::$app`
 | 
				
			||||||
 | 
					- `$this` = current `View` object
 | 
				
			||||||
@ -13,6 +13,7 @@ use Yii;
 | 
				
			|||||||
use Smarty;
 | 
					use Smarty;
 | 
				
			||||||
use yii\base\View;
 | 
					use yii\base\View;
 | 
				
			||||||
use yii\base\ViewRenderer;
 | 
					use yii\base\ViewRenderer;
 | 
				
			||||||
 | 
					use yii\helpers\Html;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * SmartyViewRenderer allows you to use Smarty templates in views.
 | 
					 * SmartyViewRenderer allows you to use Smarty templates in views.
 | 
				
			||||||
@ -48,6 +49,34 @@ class SmartyViewRenderer extends ViewRenderer
 | 
				
			|||||||
		$this->smarty = new Smarty();
 | 
							$this->smarty = new Smarty();
 | 
				
			||||||
		$this->smarty->setCompileDir(Yii::getAlias($this->compilePath));
 | 
							$this->smarty->setCompileDir(Yii::getAlias($this->compilePath));
 | 
				
			||||||
		$this->smarty->setCacheDir(Yii::getAlias($this->cachePath));
 | 
							$this->smarty->setCacheDir(Yii::getAlias($this->cachePath));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$this->smarty->registerPlugin('function', 'path', array($this, 'smarty_function_path'));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * 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");
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							array_unshift($params, $params['route']) ;
 | 
				
			||||||
 | 
							unset($params['route']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return Html::url($params);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@ -67,6 +96,10 @@ class SmartyViewRenderer extends ViewRenderer
 | 
				
			|||||||
		$ext = pathinfo($file, PATHINFO_EXTENSION);
 | 
							$ext = pathinfo($file, PATHINFO_EXTENSION);
 | 
				
			||||||
		/** @var \Smarty_Internal_Template $template */
 | 
							/** @var \Smarty_Internal_Template $template */
 | 
				
			||||||
		$template = $this->smarty->createTemplate($file, null, null, $params, true);
 | 
							$template = $this->smarty->createTemplate($file, null, null, $params, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$template->assign('app', \Yii::$app);
 | 
				
			||||||
 | 
							$template->assign('this', $view);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return $template->fetch();
 | 
							return $template->fetch();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user