An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          



Yii




Yii View Smarty Renderer



[![Latest Stable Version](https://poser.pugx.org/simialbi/view-smarty/v)](https://packagist.org/packages/simialbi/view-smarty)
[![Total Downloads](https://poser.pugx.org/simialbi/view-smarty/downloads)](https://packagist.org/packages/simialbi/view-smarty)
[![Build status](https://github.com/simialbi/view-smarty/actions/workflows/build.yml/badge.svg)](https://github.com/simialbi/view-smarty/actions/workflows/build.yml)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fview-twig%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/simialbi/view-smarty/master)
[![Static analysis](https://github.com/simialbi/view-smarty/actions/workflows/static.yml/badge.svg?branch=master)](https://github.com/simialbi/view-smarty/actions/workflows/static.yml?query=branch%3Amaster)
[![type-coverage](https://shepherd.dev/github/simialbi/view-smarty/coverage.svg)](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}


Hello!

Let's start something great with Yii3!



Don't forget to check the guide.



```

## 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.