Yii 1.1: inline-widgets-behavior

Allows to render widgets in page content
12 followers

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

Total 1 comment

#15086 report it
Alex D. at 2013/10/06 11:24am
Thanx!

Спасибо!

Leave a comment

Please to leave your comment.

Create extension