ButtonGroup.php 2.56 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\bootstrap;

Qiang Xue committed
10
use yii\helpers\ArrayHelper;
11 12 13 14 15 16 17 18 19
use yii\helpers\Html;

/**
 * ButtonGroup renders a button group bootstrap component.
 *
 * For example,
 *
 * ```php
 * // a button group with items configuration
20
 * echo ButtonGroup::widget([
Alexander Makarov committed
21 22 23 24 25
 *     'buttons' => [
 *         ['label' => 'A'],
 *         ['label' => 'B'],
 *     ]
 * ]);
26 27
 *
 * // button group with an item as a string
28
 * echo ButtonGroup::widget([
Alexander Makarov committed
29 30 31 32 33
 *     'buttons' => [
 *         Button::widget(['label' => 'A']),
 *         ['label' => 'B'],
 *     ]
 * ]);
34
 * ```
MarsuBoss committed
35 36
 * @see http://getbootstrap.com/javascript/#buttons
 * @see http://getbootstrap.com/components/#btn-groups
37 38 39 40 41
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class ButtonGroup extends Widget
{
42 43 44 45 46 47 48 49 50 51 52 53
    /**
     * @var array list of buttons. Each array element represents a single button
     * which can be specified as a string or an array of the following structure:
     *
     * - label: string, required, the button label.
     * - options: array, optional, the HTML attributes of the button.
     */
    public $buttons = [];
    /**
     * @var boolean whether to HTML-encode the button labels.
     */
    public $encodeLabels = true;
54

55 56 57 58 59 60 61 62 63
    /**
     * Initializes the widget.
     * If you override this method, make sure you call the parent implementation first.
     */
    public function init()
    {
        parent::init();
        Html::addCssClass($this->options, 'btn-group');
    }
64

65 66 67 68 69 70 71 72
    /**
     * Renders the widget.
     */
    public function run()
    {
        echo Html::tag('div', $this->renderButtons(), $this->options);
        BootstrapAsset::register($this->getView());
    }
73

74
    /**
75
     * Generates the buttons that compound the group as specified on [[buttons]].
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
     * @return string the rendering result.
     */
    protected function renderButtons()
    {
        $buttons = [];
        foreach ($this->buttons as $button) {
            if (is_array($button)) {
                $label = ArrayHelper::getValue($button, 'label');
                $options = ArrayHelper::getValue($button, 'options');
                $buttons[] = Button::widget([
                    'label' => $label,
                    'options' => $options,
                    'encodeLabel' => $this->encodeLabels,
                    'view' => $this->getView()
                ]);
            } else {
                $buttons[] = $button;
            }
        }
95

96 97
        return implode("\n", $buttons);
    }
Qiang Xue committed
98
}