GithubHelp home page GithubHelp logo

outoffspace / yii2-inline-widgets-behavior Goto Github PK

View Code? Open in Web Editor NEW

This project forked from elisdn/inline-widgets-behavior

3.0 4.0 3.0 38 KB

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

PHP 100.00%

yii2-inline-widgets-behavior's Introduction

InlineWidgetsBehavior

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

Install

Either run

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

or add

"outOFFspace/Yii2-inline-widgets-behavior": "*"
~~~
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:
~~~
<startBlock><WidgetName>[|<attribute>=<value>[;<attribute>=<value>]]<endBlock>
~~~

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

### For example:

```php
<?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:

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

yii2-inline-widgets-behavior's People

Contributors

demogorgorn avatar elisdn avatar outoffspace avatar uchenic avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yii2-inline-widgets-behavior's Issues

Не работет пагинация в data widgets

Приветствую! Не работает пагинация в ListView в виджете, встроенным вашим поведением. Подскажите как решить эту проблему?

Please fix the documentation

In the readme file there is everywhere written to use InlineWidgetBehavior. Correct is InlineWidgetsBehavior (widget_s_)

Cant pass 0 as parametr

Widget class

class PriceWidget extends Widget {
    public $id;

    public function init() {
        parent::init();
    }

    public function run() {
       return $this->id;
    }
}

Trying to call
[Price|id=0]

Expected behaviour: 0 integer

But widget returns null, not '0' integer. If I pass other integer (1,2,3) or string widget work nice.

Error if widget definition was found

After clean install I've got an error:

preg_match(): Unknown modifier 'w'
/vendor/howardeagle/yii2-inline-widgets-behavior/InlineWidgetsBehavior.php at line 132

while (preg_match('/\{' . $this->_widgetToken . ':' . $widget . '(\|([^}]*)?)?' . $this->_widgetToken . '\}/is', $text, $p)) {
    $text = str_replace($p[0], $this->_loadWidget($alias, isset($p[2]) ? $p[2] : ''), $text);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.