inline-widgets-behavior Allows to render widgets in page content

  1. Requirements
  2. Usage
  3. Resources

Allows render widgets in any page content

Requirements

Yii 1.1 or above

Usage

Insert list of allowed widgets into configuration file parameter:

return array(
    // ...
    'params'=>array(
         // ...
        'runtimeWidgets'=>array(
            'ShareWidget',
            'Comments',
            'blog.wigets.LastPostsWidget',
        }
    }
}

Create widgets

class LastPostsWidget extends CWidget
{
    public $tpl='default';
    public $limit=3;

    public function run()
    {
        $posts = Post::model()->published()->last($this->limit)->findAll();
        $this->render('LastPosts/' . $this->tpl, array(
            'posts'=>$posts,
        ));
    }
}

Attach behavior to your main controller:

class Controller extends CController
{
    public function behaviors()
    {
        return array(
            'InlineWidgetsBehavior'=>array(
                'class'=>'application.components.DInlineWidgetsBehavior',
                'location'=>'application.components.widgets', // default path (optional)               
                'widgets'=>Yii::app()->params['runtimeWidgets'],
                'startBlock'=> '{{w:',
                'endBlock'=> '}}',
             ),
        );
    }
}

Use specific templates (like BBCodes) in your pages, posts and other dinamic content and call Controller::decodeWidgets method for parse model content and rendering widgets in your views:

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

    <h2>Latest posts</h2>
    <p>{{w:LastPostsWidget}}</p>

    <h2>Latest posts (with parameters)</h2>
    <p>{{w:LastPostsWidget|limit=5}}</p>

    <h2>Latest posts (with inner caching)</h2>    
    <p>{{w:LastPostsWidget|limit=5;tpl=small;cache=300}}</p>

    <p>{{w:youtube|id=qwer12345}}</p>
    <p>{{w:flash|file=/banners/banner1.swf;w=320;h=240}}</p>
    <p>{{w:gallery|id=holiday2012}}</p>
    <p>{{w:submenu|parent=services}}</p>

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

<h1><?php echo CHtml::encode($model->title); ?></h1>
<?php echo $this->decodeWidgets($model->text); ?>

Resources

2 0
10 followers
275 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Others
Developed by: ElisDN
Created on: Jan 2, 2013
Last updated: 11 years ago

Downloads

show all

Related Extensions