array( * array( * 'label' => 'Tab one', * 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', * ), * array( * 'label' => 'Tab two', * 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...', * 'options' => array( * 'tag' => 'div', * ), * 'headerOptions' => array( * 'class' => 'my-class', * ), * ), * array( * 'label' => 'Tab with custom id', * 'content' => 'Morbi tincidunt, dui sit amet facilisis feugiat...', * 'options' => array( * 'id' => 'my-tab', * ), * ), * array( * 'label' => 'Ajax tab', * 'ajax' => array('ajax/content'), * ), * ), * 'options' => array( * 'tag' => 'div', * ), * 'itemOptions' => array( * 'tag' => 'div', * ), * 'headerOptions' => array( * 'class' => 'my-class', * ), * 'clientOptions' => array( * 'collapsible' => false, * ), * )); * ``` * * @see http://api.jqueryui.com/tabs/ * @author Alexander Kochetov * @since 2.0 */ class Tabs extends Widget { /** * @var array the HTML attributes for the widget container tag. The following special options are recognized: * * - tag: string, defaults to "div", the tag name of the container tag of this widget */ public $options = array(); /** * @var array list of tab items. Each item can be an array of the following structure: * * ~~~ * array( * 'label' => 'Tab header label', * 'content' => 'Tab item content', * 'ajax' => 'http://www.yiiframework.com', //or array('ajax/action'), * // the HTML attributes of the item container tag. This will overwrite "itemOptions". * 'options' => array(), * // the HTML attributes of the header container tag. This will overwrite "headerOptions". * 'headerOptions' = array(), * // @todo comment. * 'template' * ) * ~~~ */ public $items = array(); /** * @var array list of HTML attributes for the item container tags. This will be overwritten * by the "options" set in individual [[items]]. The following special options are recognized: * * - tag: string, defaults to "div", the tag name of the item container tags. */ public $itemOptions = array(); /** * @var array list of HTML attributes for the header container tags. This will be overwritten * by the "headerOptions" set in individual [[items]]. */ public $headerOptions = array(); /** * @var string * @todo comment. */ public $linkTemplate = '{label}'; /** * @var boolean * @todo comment. */ public $encodeLabels = true; /** * Renders the widget. */ public function run() { $options = $this->options; $tag = ArrayHelper::remove($options, 'tag', 'div'); echo Html::beginTag($tag, $options) . "\n"; echo $this->renderItems() . "\n"; echo Html::endTag($tag) . "\n"; $this->registerWidget('tabs'); } /** * Renders tab items as specified on [[items]]. * @return string the rendering result. * @throws InvalidConfigException. */ protected function renderItems() { $headers = array(); $items = array(); foreach ($this->items as $n => $item) { if (!isset($item['label'])) { throw new InvalidConfigException("The 'label' option is required."); } if (isset($item['ajax'])) { $url = $item['ajax']; } else { if (!isset($item['content'])) { throw new InvalidConfigException("The 'content' or 'ajax' option is required."); } $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', array())); $tag = ArrayHelper::remove($options, 'tag', 'div'); if (!isset($options['id'])) { $options['id'] = $this->options['id'] . '-tab' . $n; } $url = '#' . $options['id']; $items[] = Html::tag($tag, $item['content'], $options); } $headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate); $headers[] = Html::tag('li', strtr($template, array( '{label}' => $this->encodeLabels ? Html::encode($item['label']) : $item['label'], '{url}' => Html::url($url), )), $headerOptions); } return Html::tag('ul', implode("\n", $headers)) . "\n" . implode("\n", $items); } }