2017 © Pedro Peláez
 

yii2-extension yii2-inline-widgets-behavior

Allows to render widgets in page content in Yii2 Framework based projects

image

howardeagle/yii2-inline-widgets-behavior

Allows to render widgets in page content in Yii2 Framework based projects

  • Friday, January 26, 2018
  • by howardEagle
  • Repository
  • 3 Watchers
  • 2 Stars
  • 877 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 8 Forks
  • 0 Open issues
  • 1 Versions
  • 5 % Grown

The README.md

InlineWidgetsBehavior

Allows to render widgets in page content in Yii2 Framework based projects, (*1)

Install

Either run, (*2)

$ php composer.phar require --prefer-dist outOFFspace/Yii2-inline-widgets-behavior "*"

or add ~~~~ "outOFFspace/Yii2-inline-widgets-behavior": "*", (*3)

to the `require` section of your `composer.json file`.

Usage example
-------------

### Add a allowed widgets list into `config/main.php`:

```php
return [
    // ...
    'params' => [
         // ...
        'runtimeWidgets'=> [
            'common\widgets\LastPosts',
        ]
    ]
]
```

### Create widgets:

```php
class LastPostsWidget extends Widget
{
    public $tpl = 'default';

    public function run()
    {
        $posts = Post::find()->published()->all();
        return $this->render('LastPosts/' . $this->tpl, [
            'posts' => $posts,
        ]);
    }
}
```


### Attach the behavior to a main controller:

```php
use howard\behaviors\iwb\InlineWidgetsBehavior;

class DefaultController extends Controller
{
    public function behaviors()
    {
        return [
            'InlineWidgetsBehavior' => [
                'class'=> InlineWidgetsBehavior::className(),
                'namespace'=> 'common\components\widgets', // default namespace (optional)               
                'widgets'=> \Yii::$app->params['runtimeWidgets'],
                'startBlock'=> '[*',
                'endBlock'=> '*]',
             ],
        ];
    }
}
```


### You can define a global classname suffix like 'Widget':

```php
class DefaultController extends Controller
{
    public function behaviors()
    {
        return [
            'InlineWidgetsBehavior' => [
                'class' => InlineWidgetsBehavior::className(),
                'widgets' => \Yii::$app->params['runtimeWidgets'],
                'classSuffix' => 'Widget',
             ],
        ];
    }
}
```

for using short names 'LastPosts' instead of 'LastPostsWidget' :

```php
return [
    // ...
    'params' => [
         // ...
        'runtimeWidgets' => [
            'ContactsForm',
            'Comments',
            'common\widgets\LastPosts',
        ]
    ]
}
```


For insert widgets in content you can use string of this format in your text:

[|=[;=]] ~~~, (*4)

For rendering widgets in any View you must call Controller::decodeWidgets() method for model HTML content., (*5)

For example:

<?php $model->text = '
    <h2>Lorem ipsum</h2>

    <h2>Latest posts</h2>
    <p>[*LastPosts*]</p>

    <h2>Latest posts (with parameters)</h2>
    <p>[*LastPosts|tpl=small*]</p>

    <h2>Latest posts (with inner caching)</h2>
    <p>[*LastPosts|tpl=small;cache=300*]</p>

    <p>Dolor...</p>
'; ?>

<h1><?= Html::encode($model->title); ?></h1>
<?= $this->context->decodeWidgets($model->text); ?>

to have an access to the model from widgets just specify the 'model' variable in Controller::decodeWidgets() method:, (*6)

<?= $this->context->decodeWidgets($model->text, $model); ?>

The Versions

26/01 2018

dev-master

9999999-dev

Allows to render widgets in page content in Yii2 Framework based projects

  Sources   Download

BSD-3-Clause

The Requires

 

by Vovchuk Bohdan
by Elyseev Dmytryi

inline yii2 widget behaviors