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

追加の構成

Yii Twig エクステンションは、あなた自身の構文を定義して、通常のヘルパ・クラスをテンプレートに導入することを可能にしています。 構成のオプションを見ていきましょう。

グローバル (globals)

アプリケーション構成の globals 変数によって、グローバルなヘルパや変数を追加することが出来ます。 Yii のヘルパとあなた自身の変数を定義することが出来ます。

'globals' => [
    'html' => ['class' => '\yii\helpers\Html'],
    'name' => 'Carsten',
    'GridView' => ['class' => '\yii\grid\GridView'],
],

いったん構成してしまえば、テンプレートの中で以下のようにグローバルを使用することが出来ます。

Hello, {{name}}! {{ html.a('ログインしてください', 'site/login') | raw }}.

{{ GridView.widget({'dataProvider' : provider}) | raw }}

関数 (functions)

追加の関数を次のようにして定義することが出来ます。

'functions' => [
    'rot13' => 'str_rot13',
    'truncate' => '\yii\helpers\StringHelper::truncate',
    new \Twig\TwigFunction('rot14', 'str_rot13'),
    new \Twig\TwigFunction('add_*', function ($symbols, $val) {
        return $val . $symbols;
    }, ['is_safe' => ['html']]),
    'callable_add_*' => function ($symbols, $val) {
        return $val . $symbols;
    },
    'sum' => function ($a, $b) {
        return $a + $b;
    }
],

テンプレートでは、次のようにして使うことが出来ます。

{{ rot13('test') }}
{{ truncate(post.text, 100) }}
{{ rot14('test') }}
{{ add_42('answer') }}
{{ callable_add_42('test') }}
{{ sum(1, 2) }}

フィルタ (filters)

追加のフィルタをアプリケーション構成の filters オプションによって追加することが出来ます。

'filters' => [
    'jsonEncode' => '\yii\helpers\Json::htmlEncode',
    new \Twig\TwigFilter('rot13', 'str_rot13'),
    new \Twig\TwigFilter('add_*', function ($symbols, $val) {
        return $val . $symbols;
    }, ['is_safe' => ['html']]),
    'callable_rot13' => function($string) {
        return str_rot13($string);
    },
    'callable_add_*' => function ($symbols, $val) {
        return $val . $symbols;
    }
],

そうすると、テンプレートの中で、次の構文を使ってフィルタを適用することが出来ます。

{{ model|jsonEncode }}
{{ 'test'|rot13 }}
{{ 'answer'|add_42 }}
{{ 'test'|callable_rot13 }}
{{ 'answer'|callable_add_42 }}

パス

アプリケーション構成情報の twigFallbackPaths オプションによって、パスを追加することが出来ます。

'twigFallbackPaths' => [
    'layouts' => '[@app](https://github.com/app)/views/layouts' // yii2 のエイリアスが使えます
]

これをテンプレートの中で使うことが出来ます。

{% extends '[@layouts](https://github.com/layouts)/main.twig %}

プロファイリング

トレース・ログに twig-profile のデータを含めるためには、エクステンションを追加する必要があります。

'extensions' => [
    \yii\twig\Profile::class
]

プロファイラはデバッグ・モードの時だけログを吐きます。

プロファイリングの使用はパフォーマンスに影響を及ぼします。

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
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
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