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.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle