Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Yii2 Twig Laravel Package

yiisoft/yii2-twig

View on GitHub
Deep Wiki
Context7

インストール

インストールは二つの部分から成ります。すなわち、composer パッケージの取得と、アプリケーションの構成です。

エクステンションをインストールする

このエクステンションをインストールするのに推奨される方法は composer によるものです。

下記のコマンドを実行してください。

php composer.phar require --prefer-dist yiisoft/yii2-twig

または、あなたの composer.json ファイルの require セクションに、

"yiisoft/yii2-twig": "~2.0.0"

を追加してください。

アプリケーションを構成する

Twig を使い始めるためには、view コンポーネントを下記のように構成する必要があります。

[
    'components' => [
        'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '[@runtime](https://github.com/runtime)/Twig/cache',
                    // Twig のオプションの配列
                    'options' => [
                        'auto_reload' => true,
                    ],
                    'globals' => [
                        ['class' => '\yii\helpers\Html'],
                    ],
                    'uses' => ['yii\bootstrap'],
                ],
                // ...
            ],
        ],
    ],
]

構成が終った後、拡張子 .twig を持つファイルにテンプレートを作成することが出来ます。別のファイル拡張子を使いたい場合は、 それに応じてコンポーネントの構成を修正する必要があります。例えば、.html を使う場合は、下記のように構成します。

[
    'components' => [
        'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'html' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '[@runtime](https://github.com/runtime)/Twig/cache',
                    // twig のオプションの配列
                    'options' => [
                        'auto_reload' => true,
                    ],
                    'globals' => [
                        'html' => ['class' => '\yii\helpers\Html'],
                    ],
                    'uses' => ['yii\bootstrap'],
                ],
                // ...
            ],
        ],
    ],
]

Twig を使う場合は、通常のビュー・ファイルとは違って、コントローラで $this->render() を呼び出すときに拡張子を含めなければなりません。

return $this->render('renderer.twig', ['username' => 'Alex']);
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony
spatie/flare-daemon-runtime