diff --git a/ActiveField.php b/ActiveField.php index 3181a6c..4ba6033 100644 --- a/ActiveField.php +++ b/ActiveField.php @@ -7,7 +7,6 @@ namespace yii\bootstrap; -use yii\helpers\Html; use yii\helpers\ArrayHelper; /** @@ -291,6 +290,22 @@ class ActiveField extends \yii\widgets\ActiveField } /** + * 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 */ public function label($label = null, $options = []) diff --git a/ActiveForm.php b/ActiveForm.php index 9ff20d7..a574fbf 100644 --- a/ActiveForm.php +++ b/ActiveForm.php @@ -8,7 +8,6 @@ namespace yii\bootstrap; use Yii; -use yii\helpers\Html; use yii\base\InvalidConfigException; /** @@ -98,4 +97,13 @@ class ActiveForm extends \yii\widgets\ActiveForm } parent::init(); } + + /** + * @inheritdoc + * @return ActiveField the created ActiveField object + */ + public function field($model, $attribute, $options = []) + { + return parent::field($model, $attribute, $options); + } } diff --git a/Alert.php b/Alert.php index fdd8979..e5755c1 100644 --- a/Alert.php +++ b/Alert.php @@ -9,7 +9,6 @@ namespace yii\bootstrap; use Yii; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * Alert renders an alert bootstrap component. diff --git a/BaseHtml.php b/BaseHtml.php new file mode 100644 index 0000000..3b97ed2 --- /dev/null +++ b/BaseHtml.php @@ -0,0 +1,86 @@ + + * @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 diff --git a/Button.php b/Button.php index 0dc25a9..eedcc46 100644 --- a/Button.php +++ b/Button.php @@ -7,8 +7,6 @@ namespace yii\bootstrap; -use yii\helpers\Html; - /** * Button renders a bootstrap button. * diff --git a/ButtonDropdown.php b/ButtonDropdown.php index 8796fc6..5d95f5a 100644 --- a/ButtonDropdown.php +++ b/ButtonDropdown.php @@ -8,7 +8,6 @@ namespace yii\bootstrap; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * ButtonDropdown renders a group or split button dropdown bootstrap component. diff --git a/ButtonGroup.php b/ButtonGroup.php index c99312f..4bf47ef 100644 --- a/ButtonGroup.php +++ b/ButtonGroup.php @@ -8,7 +8,6 @@ namespace yii\bootstrap; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * ButtonGroup renders a button group bootstrap component. diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e351e3..7e4010c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 #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 #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 ------------------ diff --git a/Carousel.php b/Carousel.php index 7faf887..f212619 100644 --- a/Carousel.php +++ b/Carousel.php @@ -9,7 +9,6 @@ namespace yii\bootstrap; use yii\base\InvalidConfigException; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * Carousel renders a carousel bootstrap javascript component. diff --git a/Collapse.php b/Collapse.php index c45e966..0b33cd5 100644 --- a/Collapse.php +++ b/Collapse.php @@ -9,7 +9,6 @@ namespace yii\bootstrap; use yii\base\InvalidConfigException; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * Collapse renders an accordion bootstrap javascript component. diff --git a/Dropdown.php b/Dropdown.php index 35e12cd..1c28f01 100644 --- a/Dropdown.php +++ b/Dropdown.php @@ -9,8 +9,6 @@ namespace yii\bootstrap; use yii\base\InvalidConfigException; use yii\helpers\ArrayHelper; -use yii\helpers\Html; -use yii\helpers\Url; /** * Dropdown renders a Bootstrap dropdown menu component. diff --git a/Html.php b/Html.php new file mode 100644 index 0000000..a2c87e6 --- /dev/null +++ b/Html.php @@ -0,0 +1,22 @@ + + * @since 2.0.5 + */ +class Html extends BaseHtml +{ +} \ No newline at end of file diff --git a/Modal.php b/Modal.php index 70e8d41..c138f30 100644 --- a/Modal.php +++ b/Modal.php @@ -9,7 +9,6 @@ namespace yii\bootstrap; use Yii; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * Modal renders a modal window that can be toggled by clicking on a button. diff --git a/Nav.php b/Nav.php index 7195662..d94a8e2 100644 --- a/Nav.php +++ b/Nav.php @@ -10,7 +10,6 @@ namespace yii\bootstrap; use Yii; use yii\base\InvalidConfigException; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * Nav renders a nav HTML component. diff --git a/NavBar.php b/NavBar.php index 93c3cb7..aa6db21 100644 --- a/NavBar.php +++ b/NavBar.php @@ -9,7 +9,6 @@ namespace yii\bootstrap; use Yii; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * NavBar renders a navbar HTML component. diff --git a/Progress.php b/Progress.php index fbc9eb8..d80bd6b 100644 --- a/Progress.php +++ b/Progress.php @@ -9,7 +9,6 @@ namespace yii\bootstrap; use yii\base\InvalidConfigException; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * Progress renders a bootstrap progress bar component. diff --git a/Tabs.php b/Tabs.php index c31c120..ff5293a 100644 --- a/Tabs.php +++ b/Tabs.php @@ -9,7 +9,6 @@ namespace yii\bootstrap; use yii\base\InvalidConfigException; use yii\helpers\ArrayHelper; -use yii\helpers\Html; /** * Tabs renders a Tab bootstrap javascript component. diff --git a/tests/HtmlTest.php b/tests/HtmlTest.php new file mode 100644 index 0000000..6dd167b --- /dev/null +++ b/tests/HtmlTest.php @@ -0,0 +1,84 @@ +', + ], + [ + 'star', + [ + 'tag' => 'i', + 'prefix' => 'my-icon icon-', + ], + '', + ], + ]; + } + + /** + * @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', + [], + '

foo

' + ], + [ + '', + [], + '

<html>

' + ], + [ + '', + [ + 'encode' => false + ], + '

' + ], + ]; + } + + /** + * @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