{$content}
https://github.com/simialbi/view-smarty
Yii View Smarty Renderer
https://github.com/simialbi/view-smarty
smarty template-engine yii3
Last synced: 11 days ago
JSON representation
Yii View Smarty Renderer
- Host: GitHub
- URL: https://github.com/simialbi/view-smarty
- Owner: simialbi
- License: mit
- Created: 2026-03-04T10:22:07.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2026-03-05T16:41:55.000Z (4 months ago)
- Last Synced: 2026-03-05T17:50:52.333Z (4 months ago)
- Topics: smarty, template-engine, yii3
- Language: PHP
- Homepage:
- Size: 42 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
Yii View Smarty Renderer
[](https://packagist.org/packages/simialbi/view-smarty)
[](https://packagist.org/packages/simialbi/view-smarty)
[](https://github.com/simialbi/view-smarty/actions/workflows/build.yml)
[](https://dashboard.stryker-mutator.io/reports/github.com/simialbi/view-smarty/master)
[](https://github.com/simialbi/view-smarty/actions/workflows/static.yml?query=branch%3Amaster)
[](https://shepherd.dev/github/simialbi/view-smarty)
The package is an extension of the [Yii View Rendering Library](https://github.com/yiisoft/view/). This extension
provides a `SmartyTemplateRenderer` that would allow you to use [Smarty](https://www.smarty.net/) view template engine.
## Requirements
- PHP 8.1 - 8.5
## Installation
The package could be installed with [Composer](https://getcomposer.org):
```shell
composer require simialbi/view-smarty
```
## General usage
In your application, you should specify the configuration for `Smarty`
(by default, this is `config/common/di/view-smarty.php`):
```php
use Smarty\Smarty;
use Yiisoft\Aliases\Aliases;
/** @var array $params */
return [
Smarty::class => static fn (Aliases $aliases) => (new Smarty())
->setTemplateDir($aliases->get($params['smarty/smarty']['template_dir']))
->setCompileDir($aliases->get($params['smarty/smarty']['compile_dir'])),
];
```
Create a `views` directory in your project and add an alias in `config/common/aliases.php`:
```php
return [
// ...
'@views' => '@root/views',
];
```
Also add the parameters for `WebView` to `config/common/params.php`:
```php
use Psr\EventDispatcher\EventDispatcherInterface;
use Simialbi\View\Smarty\SmartyTemplateRenderer;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Definitions\DynamicReference;
use Yiisoft\Definitions\Reference;
use Yiisoft\View\WebView;
return [
// ...
'yiisoft/view' => [
'basePath' => '@views',
'defaultExtension' => 'tpl',
'renderers' => [
'tpl' => Reference::to(SmartyTemplateRenderer::class),
],
// ...
],
'smarty/smarty' => [
'template_dir' => '@views',
'compile_dir' => '@runtime/Smarty/compile',
],
'yiisoft/yii-view-renderer' => [
'viewPath' => '@views',
'layout' => '@views/layout.tpl',
'injections' => [
Reference::to(CsrfViewInjection::class),
],
],
// ...
];
```
In your Controller Action or Response Handler, make sure you use the desired Smarty template:
```php
return $this->viewRenderer->render('index.tpl');
```
### Template
All variables that were in the regular template are also available in the Smarty template.
The default main layout of the [application template](https://github.com/yiisoft/app) will look like this:
```smarty
{**
* @var App\Shared\ApplicationParams $applicationParams
* @var Yiisoft\Aliases\Aliases $aliases
* @var Yiisoft\Assets\AssetManager $assetManager
* @var string $content
* @var string|null $csrf
* @var Yiisoft\View\WebView $this
* @var Yiisoft\Router\CurrentRoute $currentRoute
* @var Yiisoft\Router\UrlGeneratorInterface $urlGenerator
*}
{$assetManager->register('App\Web\Shared\Layout\Main\MainAsset')|void}
{$this->addCssFiles($assetManager->getCssFiles())|void}
{$this->addCssStrings($assetManager->getCssStrings())|void}
{$this->addJsFiles($assetManager->getJsFiles())|void}
{$this->addJsStrings($assetManager->getJsStrings())|void}
{$this->addJsVars($assetManager->getJsVars())|void}
{$this->beginPage()}
{$this->getTitle()|escape}
{$this->head()}
{$this->beginBody()}
{$content}
{$this->endBody()}
{$this->endPage()}
```
And the view template of the main page (`site/index`) will be as follows:
```smarty
{**
* @var \App\Shared\ApplicationParams $applicationParams
* @var Yiisoft\Aliases\Aliases $aliases
* @var Yiisoft\Assets\AssetManager $assetManager
* @var string $content
* @var string|null $csrf
* @var Yiisoft\View\WebView $this
* @var Yiisoft\Router\CurrentRoute $currentRoute
* @var Yiisoft\Router\UrlGeneratorInterface $urlGenerator
*}
{set title=$applicationParams->name}
```
## License
The Yii View Smarty Renderer is free software. It is released under the terms of the MIT License.
Please see [`LICENSE`](./LICENSE.md) for more information.