Skip to content

Commit

Permalink
Add global option for widget_col, label_col and simple_col
Browse files Browse the repository at this point in the history
Fixes #148
  • Loading branch information
Florian Eckerstorfer committed Dec 17, 2013
1 parent 70a308a commit b3fb27e
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 10 deletions.
42 changes: 33 additions & 9 deletions Resources/views/Form/bootstrap.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
{% set style = style|default(bootstrap_get_style()) %}
{% set col_size = col_size|default(bootstrap_get_col_size()) %}

{% if simple_col is not defined and bootstrap_get_simple_col() %}
{% set simple_col = bootstrap_get_simple_col() %}
{% endif %}
{% if attr.simple_col is defined and attr.simple_col is not empty %}
{% set simple_col = attr.simple_col %}
{% endif %}
{% if attr.col_size is defined and attr.col_size is not empty %}
{% set col_size = attr.col_size %}
{% endif %}

{% if simple_col is defined %}
{% if simple_col is defined and simple_col %}
<div class="col-{{ col_size }}-{{ simple_col }}">
{% endif %}

Expand Down Expand Up @@ -235,8 +238,8 @@

{% set class = '' %}
{% if align_with_widget is defined or attr.align_with_widget is defined %}
{% set widget_col = widget_col|default(10) %}
{% set label_col = label_col|default(2) %}
{% set widget_col = widget_col|default(bootstrap_get_widget_col()) %}
{% set label_col = label_col|default(bootstrap_get_label_col()) %}
{% set class = 'col-' ~ col_size ~ '-' ~ widget_col ~ ' col-' ~ col_size ~ '-offset-' ~ label_col %}
<div class="form-group row{% if not form.vars.valid %} has-error{% endif %}">
<div class="{{ class }}">
Expand Down Expand Up @@ -291,8 +294,8 @@
{% endif %}

{% if align_with_widget is defined or attr.align_with_widget is defined %}
{% set widget_col = widget_col|default(10) %}
{% set label_col = label_col|default(2) %}
{% set widget_col = widget_col|default(bootstrap_get_widget_col()) %}
{% set label_col = label_col|default(bootstrap_get_label_col()) %}
{% set class = ' col-'~ col_size ~ '-' ~ widget_col ~ ' col-' ~ col_size ~ '-offset-' ~ label_col %}
<div class="form-group row{% if not form.vars.valid %} has-error{% endif %}">
<div class="{{ class }}">
Expand Down Expand Up @@ -560,8 +563,8 @@
{% set col_size = attr.col_size %}
{% endif %}

{% set label_col = label_col|default(2) %}
{% set widget_col = widget_col|default(10) %}
{% set label_col = label_col|default(bootstrap_get_label_col()) %}
{% set widget_col = widget_col|default(bootstrap_get_widget_col()) %}

{% set row_class = '' %}
{% if style == 'horizontal' %}
Expand Down Expand Up @@ -644,8 +647,8 @@
{% set col_size = attr.col_size %}
{% endif %}

{% set label_col = label_col|default(2) %}
{% set widget_col = widget_col|default(10) %}
{% set label_col = label_col|default(bootstrap_get_label_col()) %}
{% set widget_col = widget_col|default(bootstrap_get_widget_col()) %}

{% if style == 'horizontal' %}
<div class="form-group">
Expand Down Expand Up @@ -693,6 +696,18 @@
{{ bootstrap_set_col_size(col_size) }}
{% endif %}

{% if widget_col is defined %}
{{ bootstrap_set_widget_col(widget_col) }}
{% endif %}

{% if label_col is defined %}
{{ bootstrap_set_label_col(label_col) }}
{% endif %}

{% if simple_col is defined %}
{{ bootstrap_set_simple_col(simple_col) }}
{% endif %}

{% if attr.role is not defined or attr.role is empty %}
{% set attr = attr|merge({ 'role': 'form' }) %}
{% endif %}
Expand All @@ -716,6 +731,15 @@
{% if bootstrap_get_col_size() %}
{{ bootstrap_set_col_size('lg') }}
{% endif %}
{% if bootstrap_get_widget_col() %}
{{ bootstrap_set_widget_col(10) }}
{% endif %}
{% if bootstrap_get_label_col() %}
{{ bootstrap_set_label_col(2) }}
{% endif %}
{% if bootstrap_get_simple_col() %}
{{ bootstrap_set_simple_col(false) }}
{% endif %}
{% endspaceless %}
{% endblock form_end %}

Expand Down
32 changes: 31 additions & 1 deletion Tests/Twig/BootstrapFormExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function setUp()
*/
public function testGetFunctions()
{
$this->assertCount(7, $this->extension->getFunctions());
$this->assertCount(13, $this->extension->getFunctions());
}

/**
Expand All @@ -67,6 +67,36 @@ public function testSetColSizeGetColSize()
$this->assertEquals('sm', $this->extension->getColSize());
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapFormExtension::setWidgetCol()
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapFormExtension::getWidgetCol()
*/
public function testSetWidgetColGetWidgetCol()
{
$this->extension->setWidgetCol(5);
$this->assertEquals(5, $this->extension->getWidgetCol());
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapFormExtension::setLabelCol()
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapFormExtension::getLabelCol()
*/
public function testSetLabelColGetLabelCol()
{
$this->extension->setLabelCol(4);
$this->assertEquals(4, $this->extension->getLabelCol());
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapFormExtension::setSimpleCol()
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapFormExtension::getSimpleCol()
*/
public function testSetSimpleColGetSimpleCol()
{
$this->extension->setSimpleCol(8);
$this->assertEquals(8, $this->extension->getSimpleCol());
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapFormExtension::getName()
*/
Expand Down
75 changes: 75 additions & 0 deletions Twig/BootstrapFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class BootstrapFormExtension extends \Twig_Extension
/** @var string */
private $colSize = 'lg';

/** @var integer */
private $widgetCol = 10;

/** @var integer */
private $labelCol = 2;

/** @var integer */
private $simpleCol = false;

/**
* {@inheritdoc}
*/
Expand All @@ -34,6 +43,12 @@ public function getFunctions()
new \Twig_SimpleFunction('bootstrap_get_style', array($this, 'getStyle')),
new \Twig_SimpleFunction('bootstrap_set_col_size', array($this, 'setColSize')),
new \Twig_SimpleFunction('bootstrap_get_col_size', array($this, 'getColSize')),
new \Twig_SimpleFunction('bootstrap_set_widget_col', array($this, 'setWidgetCol')),
new \Twig_SimpleFunction('bootstrap_get_widget_col', array($this, 'getWidgetCol')),
new \Twig_SimpleFunction('bootstrap_set_label_col', array($this, 'setLabelCol')),
new \Twig_SimpleFunction('bootstrap_get_label_col', array($this, 'getLabelCol')),
new \Twig_SimpleFunction('bootstrap_set_simple_col', array($this, 'setSimpleCol')),
new \Twig_SimpleFunction('bootstrap_get_simple_col', array($this, 'getSimpleCol')),
'checkbox_row' => new \Twig_Function_Node(
'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode',
array('is_safe' => array('html'))
Expand Down Expand Up @@ -96,4 +111,64 @@ public function getColSize()
{
return $this->colSize;
}

/**
* Sets the number of columns of widgets.
*
* @param integer $widgetCol Number of columns.
*/
public function setWidgetCol($widgetCol)
{
$this->widgetCol = $widgetCol;
}

/**
* Returns the number of columns of widgets.
*
* @return integer Number of columns.
*/
public function getWidgetCol()
{
return $this->widgetCol;
}

/**
* Sets the number of columns of labels.
*
* @param integer $labelCol Number of columns.
*/
public function setLabelCol($labelCol)
{
$this->labelCol = $labelCol;
}

/**
* Returns the number of columns of labels.
*
* @return integer Number of columns.
*/
public function getLabelCol()
{
return $this->labelCol;
}

/**
* Sets the number of columns of simple widgets.
*
* @param integer $simpleCol Number of columns.
*/
public function setSimpleCol($simpleCol)
{
$this->simpleCol = $simpleCol;
}

/**
* Returns the number of columns of simple widgets.
*
* @return integer Number of columns.
*/
public function getSimpleCol()
{
return $this->simpleCol;
}
}

0 comments on commit b3fb27e

Please sign in to comment.