mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	Twig: added path() function, exposed app and this in templates
This commit is contained in:
		@ -18,6 +18,7 @@ array(
 | 
				
			|||||||
				),
 | 
									),
 | 
				
			||||||
				'twig' => array(
 | 
									'twig' => array(
 | 
				
			||||||
					'class' => 'yii\renderers\TwigViewRenderer',
 | 
										'class' => 'yii\renderers\TwigViewRenderer',
 | 
				
			||||||
 | 
										'twigPath' => '@app/vendors/Twig',
 | 
				
			||||||
				),
 | 
									),
 | 
				
			||||||
				// ...
 | 
									// ...
 | 
				
			||||||
			),
 | 
								),
 | 
				
			||||||
@ -38,6 +39,21 @@ or `$this->renderPartial()` from your controller:
 | 
				
			|||||||
echo $this->render('renderer.twig', array('username' => 'Alex'));
 | 
					echo $this->render('renderer.twig', array('username' => 'Alex'));
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Additional functions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Additionally to regular Twig syntax the following is available in Yii:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```php
 | 
				
			||||||
 | 
					<a href="{{ path('blog/view', {'alias' : post.alias}) }}">{{ post.title }}</a>
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					path function calls `Html::url()` internally.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Additional variables
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- `app` = `\Yii::$app`
 | 
				
			||||||
 | 
					- `this` = current `View` object
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Smarty
 | 
					Smarty
 | 
				
			||||||
------
 | 
					------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -12,6 +12,7 @@ namespace yii\renderers;
 | 
				
			|||||||
use Yii;
 | 
					use Yii;
 | 
				
			||||||
use yii\base\View;
 | 
					use yii\base\View;
 | 
				
			||||||
use yii\base\ViewRenderer;
 | 
					use yii\base\ViewRenderer;
 | 
				
			||||||
 | 
					use yii\helpers\Html;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * TwigViewRenderer allows you to use Twig templates in views.
 | 
					 * TwigViewRenderer allows you to use Twig templates in views.
 | 
				
			||||||
@ -53,6 +54,12 @@ class TwigViewRenderer extends ViewRenderer
 | 
				
			|||||||
		$this->twig = new \Twig_Environment($loader, array_merge(array(
 | 
							$this->twig = new \Twig_Environment($loader, array_merge(array(
 | 
				
			||||||
			'cache' => Yii::getAlias($this->cachePath),
 | 
								'cache' => Yii::getAlias($this->cachePath),
 | 
				
			||||||
		), $this->options));
 | 
							), $this->options));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$this->twig->addFunction('path', new \Twig_Function_Function(function($path, $args = array()){
 | 
				
			||||||
 | 
								return Html::url(array_merge(array($path), $args));
 | 
				
			||||||
 | 
							}));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$this->twig->addGlobal('app', \Yii::$app);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@ -69,6 +76,7 @@ class TwigViewRenderer extends ViewRenderer
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public function render($view, $file, $params)
 | 
						public function render($view, $file, $params)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
							$this->twig->addGlobal('this', $view);
 | 
				
			||||||
		return $this->twig->render(file_get_contents($file), $params);
 | 
							return $this->twig->render(file_get_contents($file), $params);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user