1. Go to this page and download the library: Download yiisoft/yii-view library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yiisoft / yii-view example snippets
/**
* @var \Yiisoft\Aliases\Aliases $aliases
* @var \Yiisoft\DataResponse\DataResponseFactoryInterface $dataResponseFactory
* @var \Yiisoft\View\WebView $webView
*/
$viewRenderer = new \Yiisoft\Yii\View\Renderer\ViewRenderer(
$dataResponseFactory,
$aliases,
$webView,
'/path/to/views', // Full path to the directory of view templates or its alias.
'layouts/main.php', // Default is null, which means not to use a layout.
);
// Rendering a view with a layout.
$response = $viewRenderer->render('site/page', [
'parameter-name' => 'parameter-value',
]);
$viewRenderer = $viewRenderer->withLayout(null);
// Rendering a view without a layout.
$response = $viewRenderer->render('site/page', [
'parameter-name' => 'parameter-value',
]);
// Rendering a view without a layout.
$response = $viewRenderer->renderPartial('site/page', [
'parameter-name' => 'parameter-value',
]);
// Rendering a view with a layout.
$result = $viewRenderer->renderAsString('site/page', [
'parameter-name' => 'parameter-value',
]);
// Rendering a view without a layout.
$result = $viewRenderer->renderPartialAsString('site/page', [
'parameter-name' => 'parameter-value',
]);
use Psr\Http\Message\ResponseInterface;
use Yiisoft\Yii\View\Renderer\ViewRenderer;
class SiteController
{
private ViewRenderer $viewRenderer;
public function __construct(ViewRenderer $viewRenderer)
{
// Specify the name of the controller:
$this->viewRenderer = $viewRenderer->withControllerName('site');
// or specify an instance of the controller:
//$this->viewRenderer = $viewRenderer->withController($this);
}
public function index(): ResponseInterface
{
return $this->viewRenderer->render('index');
}
public function contact(): ResponseInterface
{
// Some actions.
return $this->viewRenderer->render('contact', [
'parameter-name' => 'parameter-value',
]);
}
}
use Yiisoft\Yii\View\Renderer\CommonParametersInjectionInterface;
use Yiisoft\Yii\View\Renderer\LayoutParametersInjectionInterface;
final class MyParametersInjection implements
CommonParametersInjectionInterface,
LayoutParametersInjectionInterface
{
// Pass both to view template and to layout
public function getCommonParameters(): array
{
return [
'common-parameter-name' => 'common-parameter-value',
];
}
// Pass only to layout
public function getLayoutParameters(): array
{
return [
'layout-parameter-name' => 'layout-parameter-value',
];
}
}
use Yiisoft\Html\Html;
use Yiisoft\View\WebView;
use Yiisoft\Yii\View\Renderer\LinkTagsInjectionInterface;
use Yiisoft\Yii\View\Renderer\MetaTagsInjectionInterface;
final class MyTagsInjection implements
LinkTagsInjectionInterface,
MetaTagsInjectionInterface
{
public function getLinkTags(): array
{
return [
Html::link()->toCssFile('/main.css'),
'favicon' => Html::link('/myicon.png', [
'rel' => 'icon',
'type' => 'image/png',
]),
'themeCss' => [
'__position' => WebView::POSITION_END,
Html::link()->toCssFile('/theme.css'),
],
'userCss' => [
'__position' => WebView::POSITION_BEGIN,
'rel' => 'stylesheet',
'href' => '/user.css',
],
];
}
public function getMetaTags(): array
{
return [
Html::meta()
->name('http-equiv')
->content('public'),
'noindex' => Html::meta()
->name('robots')
->content('noindex'),
[
'name' => 'description',
'content' => 'This website is about funny raccoons.',
],
'keywords' => [
'name' => 'keywords',
'content' => 'yii,framework',
],
];
}
}
$parameters = new MyParametersInjection();
$tags = new MyTagsInjection();
$viewRenderer = $viewRenderer->withInjections($parameters, $tags);
// Or append it:
$viewRenderer = $viewRenderer->withAddedInjections($parameters, $tags);
use Yiisoft\Yii\View\Renderer\ViewRenderer;
use Yiisoft\Yii\View\Renderer\InjectionContainer\InjectionContainer;
/**
* @var Psr\Container\ContainerInterface $container
*/
$viewRenderer = new ViewRenderer(
injectionContainer: new InjectionContainer($container)
)
'yiisoft/yii-view-renderer' => [
// The full path to the directory of views or its alias.
// If null, relative view paths in `ViewRenderer::render()` is not available.
'viewPath' => null,
// The full path to the layout file to be applied to views.
// If null, the layout will not be applied.
'layout' => null,
// The injection instances or class names.
'injections' => [],
],
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.