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

Additional Configuration

Yii Twig extension allows you to define your own syntax and bring regular helper classes into templates. Let's review configuration options.

Globals

You can add global helpers or values via the application configuration's globals variable. You can define both Yii helpers and your own variables there:

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

Once configured, in your template you can use the globals in the following way:

Hello, {{name}}! {{ html.a('Please login', 'site/login') | raw }}.

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

Functions

You can define additional functions like the following:

'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;
    }
],

In template they could be used like the following:

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

Filters

Additional filters may be added via the application configuration's filters option:

'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;
    }
],

Then in the template you can apply filter using the following syntax:

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

Paths

Additional paths may be added via the application configuration's twigFallbackPaths option:

'twigFallbackPaths' => [
    'layouts' => '[@app](https://github.com/app)/views/layouts' //it is possible to use yii2-alises
]

Then they could be used in a template:

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

Profiling

To include twig-profile data in trace log you need to add the extension

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

Profile writes log only debug mode.

Using a profile affects performance.

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