cvepdb/laravel-widgets

This package is abandoned and no longer maintained. The author suggests using the cvepdb/laravel-widgets package instead.

Simple widgets system for laravel framework

This package has no released version yet, and little information is available.


README

Build Status

http://laravel5pingpong.blogspot.fr/2016/08/widget_18.html

Installation

Open your composer.json file and add the new required package.

"cvepdb/laravel-widget" : "~2.3"

Next, open your terminal and run composer update. After composer updated, add new service provider in config/app.php :

'CVEPDB\Widgets\App\Providers\WidgetsServiceProvider',

And add facade in the same file

'Widget' => 'CVEPDB\Widgets\App\Facades\WidgetsFacade'

Done.

What's New!

Subscribe widget: It's a new way to register widget using a specified class. For example: Widget::subscribe('WidgetSubscriber');

class WidgetSubscriber {

    public function subscribe($widget)
    {
        $widget->register('image', __CLASS__ .'@image');
    }

    public function image()
    {
        return 'Your handler here';
    }
}

You can also specified which method to handle subscriber of widget.

Widget::subscribe('WidgetSubscriber@handle');

class WidgetSubscriber {

    public function handle($widget)
    {
        $widget->register('image', __CLASS__ .'@image');
    }

    public function image()
    {
        return 'Your handler here';
    }
}

Registering A Widget

By default you can register a widget in app/widgets.php, that file will autoload automatically. Via Closure.

app/widgets.php

Widget::register('small', function($contents)
{
    return "<small>{$contents}</small>";
});

Widget::register('view', function($view, $data = array(), $mergeData = array()
{
    return View::make($view, $data, $mergeData)->render();
});

Via Class Name.

By default will call register method.

class MyWidget {

    public function register($contents, $attributes = array())
    {
        $attributes = HTML::attributes($attributes);

        return "<h1{$attributes}>{$contents}</h1>";
    }

} 

Widget::register('h1', 'MyWidget');

Via Class Name with the specified method.

class TagCreator {

    public function create($tag, $contents, $attributes = array())
    {
        $attributes = HTML::attributes($attributes);

        return "<{$tag}{$attributes}>{$contents}</{$tag}>";
    }

} 

class HTMLWidget {

    protected $tag;

    public function __construct(TagCreator $tag)
    {
        $this->tag = $tag;
    }

    public function p($contents, $attributes = array())
    {
        return $this->tag->create('p', $contents, $attributes);
    }

    public function div($contents, $attributes = array())
    {
        return $this->tag->create('div', $contents, $attributes);
    }
}

Widget::register('p', 'HTMLWidget@p');

Widget::register('div', 'HTMLWidget@div');

Calling A Widget

Widget::get('small', array('My Content'));

Widget::call('small', array('My Content'));

Widget::small('My Content');

Widget::p('My Content');

Widget::div('My Content');

Widget::h1('My Content');

On view you can call like this.

@small('My Content')

@view('users.show', $data, $mergeData)

@h1('Welcome!')

@p('Hello World', array('class' => 'page-header'));

@div('Lorem ipsum', array('class' => 'alert alert-warning'));

Grouping A Widget

It is very easy to group widget. you only need to specify the group name and specify an array of the names of the widgets that will be grouped.

Widget::register('calendar', 'SidebarWidget@calendar')

Widget::register('archive', 'SidebarWidget@archive')

Widget::group('sidebar', array('calendar', 'archive'));

To call a group of widgets is the same as calling the widget.

Widget::sidebar();

If you want to send parameters to the widget that is in the group, you can call it like this.

Widget::sidebar(
    array('your-first-param', 'your-second-param'),
    array('first-param-for-second-widget', 'the-second')
);

On view you can call a group of widgets is same as calling the widget.

@sidebar()

@sidebar(array('first-param'), array('first-param-for-second-widget'))