Commit 5f1b8ab7 authored by Paul Klimov's avatar Paul Klimov

Merge pull request #34 from klimov-paul/html-helper

`yii\bootstrap\Html` helper added
parents f387e6eb d044ce5a
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
namespace yii\bootstrap; namespace yii\bootstrap;
use yii\helpers\Html;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
/** /**
...@@ -290,6 +289,22 @@ class ActiveField extends \yii\widgets\ActiveField ...@@ -290,6 +289,22 @@ class ActiveField extends \yii\widgets\ActiveField
return $this; return $this;
} }
/**
* Renders Bootstrap static form control.
* @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. There are also a special options:
*
* - encode: boolean, whether value should be HTML-encoded or not.
*
* @return $this the field object itself
*/
public function staticControl($options = [])
{
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeStaticControl($this->model, $this->attribute, $options);
return $this;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace yii\bootstrap; namespace yii\bootstrap;
use Yii; use Yii;
use yii\helpers\Html;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
/** /**
...@@ -98,4 +97,13 @@ class ActiveForm extends \yii\widgets\ActiveForm ...@@ -98,4 +97,13 @@ class ActiveForm extends \yii\widgets\ActiveForm
} }
parent::init(); parent::init();
} }
/**
* @inheritdoc
* @return ActiveField the created ActiveField object
*/
public function field($model, $attribute, $options = [])
{
return parent::field($model, $attribute, $options);
}
} }
...@@ -9,7 +9,6 @@ namespace yii\bootstrap; ...@@ -9,7 +9,6 @@ namespace yii\bootstrap;
use Yii; use Yii;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* Alert renders an alert bootstrap component. * Alert renders an alert bootstrap component.
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\helpers\ArrayHelper;
/**
* BaseHtml provides concrete implementation for [[Html]].
*
* Do not use BaseHtml. Use [[Html]] instead.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0.5
*/
class BaseHtml extends \yii\helpers\Html
{
/**
* Composes icon HTML.
* @see http://getbootstrap.com/components/#glyphicons
* @param string $name icon short name, for example: 'star'
* @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. There are also a special options:
*
* - tag: string, tag to be rendered, by default 'span' is used.
* - prefix: string, prefix which should be used to compose tag class, by default 'glyphicon glyphicon-' is used.
*
* @return string icon HTML.
*/
public static function icon($name, $options = [])
{
$tag = ArrayHelper::remove($options, 'tag', 'span');
$classPrefix = ArrayHelper::remove($options, 'prefix', 'glyphicon glyphicon-');
static::addCssClass($options, $classPrefix . $name);
return static::tag($tag, '', $options);
}
/**
* Renders Bootstrap static form control.
* @see http://getbootstrap.com/css/#forms-controls-static
* By default value will be HTML-encoded using [[encode()]], you may control this behavior
* via 'encode' option.
* @param string $value static control value.
* @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. There are also a special options:
*
* - encode: boolean, whether value should be HTML-encoded or not.
*
* @return string generated HTML
*/
public static function staticControl($value, $options = [])
{
static::addCssClass($options, 'form-control-static');
$value = (string) $value;
if (isset($options['encode'])) {
$encode = $options['encode'];
unset($options['encode']);
} else {
$encode = true;
}
return static::tag('p', $encode ? static::encode($value) : $value, $options);
}
/**
* Generates a Bootstrap static form control for the given model attribute.
* @param \yii\base\Model $model the model object.
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
* about attribute expression.
* @param array $options the tag options in terms of name-value pairs. See [[staticControl()]] for details.
* @return string generated HTML
*/
public static function activeStaticControl($model, $attribute, $options = [])
{
if (isset($options['value'])) {
$value = $options['value'];
unset($options['value']);
} else {
$value = static::getAttributeValue($model, $attribute);
}
return static::staticControl($value, $options);
}
}
\ No newline at end of file
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
namespace yii\bootstrap; namespace yii\bootstrap;
use yii\helpers\Html;
/** /**
* Button renders a bootstrap button. * Button renders a bootstrap button.
* *
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace yii\bootstrap; namespace yii\bootstrap;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* ButtonDropdown renders a group or split button dropdown bootstrap component. * ButtonDropdown renders a group or split button dropdown bootstrap component.
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace yii\bootstrap; namespace yii\bootstrap;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* ButtonGroup renders a button group bootstrap component. * ButtonGroup renders a button group bootstrap component.
......
...@@ -7,6 +7,9 @@ Yii Framework 2 bootstrap extension Change Log ...@@ -7,6 +7,9 @@ Yii Framework 2 bootstrap extension Change Log
- Enh #38: Added object support for `content` option in `Collapse` class (pana1990, ItsReddi) - Enh #38: Added object support for `content` option in `Collapse` class (pana1990, ItsReddi)
- Enh #40: Added `visible` option to `yii\bootstrap\Tab` widget items (klimov-paul) - Enh #40: Added `visible` option to `yii\bootstrap\Tab` widget items (klimov-paul)
- Enh #41: Added `submenuOptions` support at `yii\bootstrap\Dropdown` (spikyjt, klimov-paul) - Enh #41: Added `submenuOptions` support at `yii\bootstrap\Dropdown` (spikyjt, klimov-paul)
- Enh #42: Added support for the glyphicons via `yii\bootstrap\Html::icon()` (klimov-paul)
- Enh #43: Added support for the static form controls via `yii\bootstrap\Html` (klimov-paul)
2.0.4 May 10, 2015 2.0.4 May 10, 2015
------------------ ------------------
......
...@@ -9,7 +9,6 @@ namespace yii\bootstrap; ...@@ -9,7 +9,6 @@ namespace yii\bootstrap;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* Carousel renders a carousel bootstrap javascript component. * Carousel renders a carousel bootstrap javascript component.
......
...@@ -9,7 +9,6 @@ namespace yii\bootstrap; ...@@ -9,7 +9,6 @@ namespace yii\bootstrap;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* Collapse renders an accordion bootstrap javascript component. * Collapse renders an accordion bootstrap javascript component.
......
...@@ -9,8 +9,6 @@ namespace yii\bootstrap; ...@@ -9,8 +9,6 @@ namespace yii\bootstrap;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
/** /**
* Dropdown renders a Bootstrap dropdown menu component. * Dropdown renders a Bootstrap dropdown menu component.
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
/**
* Html is an enhanced version of [[\yii\helpers\Html]] helper class dedicated to Bootstrap needs.
* This class inherits all functionality available at [[\yii\helpers\Html]] and can be used as substitute.
*
* Attention: do not confuse [[\yii\bootstrap\Html]] and [[\yii\helpers\Html]], be careful in which class
* you are using inside your views.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0.5
*/
class Html extends BaseHtml
{
}
\ No newline at end of file
...@@ -9,7 +9,6 @@ namespace yii\bootstrap; ...@@ -9,7 +9,6 @@ namespace yii\bootstrap;
use Yii; use Yii;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* Modal renders a modal window that can be toggled by clicking on a button. * Modal renders a modal window that can be toggled by clicking on a button.
......
...@@ -10,7 +10,6 @@ namespace yii\bootstrap; ...@@ -10,7 +10,6 @@ namespace yii\bootstrap;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* Nav renders a nav HTML component. * Nav renders a nav HTML component.
......
...@@ -9,7 +9,6 @@ namespace yii\bootstrap; ...@@ -9,7 +9,6 @@ namespace yii\bootstrap;
use Yii; use Yii;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* NavBar renders a navbar HTML component. * NavBar renders a navbar HTML component.
......
...@@ -9,7 +9,6 @@ namespace yii\bootstrap; ...@@ -9,7 +9,6 @@ namespace yii\bootstrap;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* Progress renders a bootstrap progress bar component. * Progress renders a bootstrap progress bar component.
......
...@@ -9,7 +9,6 @@ namespace yii\bootstrap; ...@@ -9,7 +9,6 @@ namespace yii\bootstrap;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
* Tabs renders a Tab bootstrap javascript component. * Tabs renders a Tab bootstrap javascript component.
......
<?php
namespace yiiunit\extensions\bootstrap;
use yii\bootstrap\Html;
/**
* @group bootstrap
*/
class HtmlTest extends TestCase
{
/**
* Data provider for [[testIcon()]]
* @return array test data
*/
public function dataProviderIcon()
{
return [
[
'star',
[],
'<span class="glyphicon glyphicon-star"></span>',
],
[
'star',
[
'tag' => 'i',
'prefix' => 'my-icon icon-',
],
'<i class="my-icon icon-star"></i>',
],
];
}
/**
* @dataProvider dataProviderIcon
*
* @param $name
* @param $options
* @param $expectedHtml
*/
public function testIcon($name, array $options, $expectedHtml)
{
$this->assertEquals($expectedHtml, Html::icon($name, $options));
}
/**
* @return array
*/
public function dataProviderStaticControl()
{
return [
[
'foo',
[],
'<p class="form-control-static">foo</p>'
],
[
'<html>',
[],
'<p class="form-control-static">&lt;html&gt;</p>'
],
[
'<html></html>',
[
'encode' => false
],
'<p class="form-control-static"><html></html></p>'
],
];
}
/**
* @dataProvider dataProviderStaticControl
*
* @param string $value
* @param array $options
* @param string $expectedHtml
*/
public function testStaticControl($value, array $options, $expectedHtml)
{
$this->assertEquals($expectedHtml, Html::staticControl($value, $options));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment