Skip to content

Commit a050160

Browse files
committed
bug symfony#11278 Remove Spaceless Blocks From Twig Templates (chrisguitarguy)
This PR was submitted for the master branch but it was merged into the 2.4 branch instead (closes symfony#11278). Discussion ---------- Remove Spaceless Blocks From Twig Templates Leaving it in can only mangle values from data bound to the form. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#11277 | License | MIT | Doc PR | The tests pass here, but it doesn't seem like any tests really cover the actual rendering. Commits ------- 793a083 Remove Spaceless Blocks From Twig Templates
2 parents 56b0139 + 793a083 commit a050160

File tree

5 files changed

+88
-82
lines changed

5 files changed

+88
-82
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{%- endblock form_widget %}
1010

1111
{% block form_widget_simple -%}
12-
{%- set type = type|default('text') -%}
12+
{% set type = type|default('text') -%}
1313
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
1414
{%- endblock form_widget_simple %}
1515

@@ -44,15 +44,15 @@
4444

4545
{% block choice_widget_expanded -%}
4646
<div {{ block('widget_container_attributes') }}>
47-
{% for child in form %}
47+
{%- for child in form %}
4848
{{- form_widget(child) -}}
4949
{{- form_label(child) -}}
50-
{% endfor %}
50+
{% endfor -%}
5151
</div>
52-
{%- endblock choice_widget_expanded %}
52+
{% endblock choice_widget_expanded %}
5353

5454
{% block choice_widget_collapsed -%}
55-
{% if required and empty_value is none and not empty_value_in_choices and not multiple %}
55+
{% if required and empty_value is none and not empty_value_in_choices and not multiple -%}
5656
{% set required = false %}
5757
{% endif %}
5858
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
@@ -95,20 +95,20 @@
9595
{% block datetime_widget -%}
9696
{% if widget == 'single_text' %}
9797
{{- block('form_widget_simple') -}}
98-
{% else -%}
98+
{% else %}
9999
<div {{ block('widget_container_attributes') }}>
100100
{{- form_errors(form.date) -}}
101101
{{- form_errors(form.time) -}}
102102
{{- form_widget(form.date) -}}
103103
{{- form_widget(form.time) -}}
104104
</div>
105-
{%- endif %}
105+
{% endif %}
106106
{%- endblock datetime_widget %}
107107

108108
{% block date_widget -%}
109-
{% if widget == 'single_text' -%}
110-
{{ block('form_widget_simple') }}
111-
{%- else -%}
109+
{% if widget == 'single_text' %}
110+
{{- block('form_widget_simple') -}}
111+
{% else -%}
112112
<div {{ block('widget_container_attributes') }}>
113113
{{- date_pattern|replace({
114114
'{{ year }}': form_widget(form.year),
@@ -120,10 +120,10 @@
120120
{%- endblock date_widget %}
121121

122122
{% block time_widget -%}
123-
{% if widget == 'single_text' -%}
124-
{{ block('form_widget_simple') }}
125-
{%- else -%}
126-
{% set vars = widget == 'text' ? { 'attr': { 'size': 1 }} : {} -%}
123+
{% if widget == 'single_text' %}
124+
{{- block('form_widget_simple') -}}
125+
{% else -%}
126+
{% set vars = widget == 'text' ? { 'attr': { 'size': 1 }} : {} %}
127127
<div {{ block('widget_container_attributes') }}>
128128
{{ form_widget(form.hour, vars) }}{% if with_minutes %}:{{ form_widget(form.minute, vars) }}{% endif %}{% if with_seconds %}:{{ form_widget(form.second, vars) }}{% endif %}
129129
</div>
@@ -132,8 +132,8 @@
132132

133133
{% block number_widget -%}
134134
{# type="number" doesn't work with floats #}
135-
{%- set type = type|default('text') -%}
136-
{{ block('form_widget_simple') }}
135+
{% set type = type|default('text') %}
136+
{{- block('form_widget_simple') -}}
137137
{%- endblock number_widget %}
138138

139139
{% block integer_widget -%}
@@ -146,33 +146,33 @@
146146
{%- endblock money_widget %}
147147

148148
{% block url_widget -%}
149-
{% set type = type|default('url') -%}
150-
{{ block('form_widget_simple') }}
149+
{% set type = type|default('url') %}
150+
{{- block('form_widget_simple') -}}
151151
{%- endblock url_widget %}
152152

153153
{% block search_widget -%}
154-
{% set type = type|default('search') -%}
155-
{{ block('form_widget_simple') }}
154+
{% set type = type|default('search') %}
155+
{{- block('form_widget_simple') -}}
156156
{%- endblock search_widget %}
157157

158158
{% block percent_widget -%}
159-
{% set type = type|default('text') -%}
160-
{{ block('form_widget_simple') }} %
159+
{% set type = type|default('text') %}
160+
{{- block('form_widget_simple') -}} %
161161
{%- endblock percent_widget %}
162162

163163
{% block password_widget -%}
164-
{% set type = type|default('password') -%}
164+
{% set type = type|default('password') %}
165165
{{ block('form_widget_simple') }}
166166
{%- endblock password_widget %}
167167

168168
{% block hidden_widget -%}
169-
{% set type = type|default('hidden') -%}
170-
{{ block('form_widget_simple') }}
171-
{%- endblock hidden_widget %}
169+
{% set type = type|default('hidden') %}
170+
{{- block('form_widget_simple') -}}
171+
{%- endblock hidden_widget -%}
172172

173173
{% block email_widget -%}
174-
{% set type = type|default('email') -%}
175-
{{ block('form_widget_simple') }}
174+
{% set type = type|default('email') %}
175+
{{- block('form_widget_simple') -}}
176176
{%- endblock email_widget %}
177177

178178
{% block button_widget -%}
@@ -183,26 +183,26 @@
183183
{%- endblock button_widget %}
184184

185185
{% block submit_widget -%}
186-
{% set type = type|default('submit') -%}
187-
{{ block('button_widget') }}
186+
{% set type = type|default('submit') %}
187+
{{- block('button_widget') -}}
188188
{%- endblock submit_widget %}
189189

190190
{% block reset_widget -%}
191-
{% set type = type|default('reset') -%}
192-
{{ block('button_widget') }}
191+
{% set type = type|default('reset') %}
192+
{{- block('button_widget') -}}
193193
{%- endblock reset_widget %}
194194

195195
{# Labels #}
196196

197197
{% block form_label -%}
198-
{% if label is not sameas(false) %}
199-
{%- if not compound -%}
198+
{% if label is not sameas(false) -%}
199+
{% if not compound -%}
200200
{% set label_attr = label_attr|merge({'for': id}) %}
201-
{%- endif -%}
202-
{%- if required -%}
201+
{%- endif %}
202+
{% if required -%}
203203
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
204-
{%- endif -%}
205-
{%- if label is empty -%}
204+
{%- endif %}
205+
{% if label is empty -%}
206206
{% set label = name|humanize %}
207207
{%- endif -%}
208208
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
@@ -218,7 +218,7 @@
218218
No need to render the errors here, as all errors are mapped
219219
to the first child (see RepeatedTypeValidatorExtension).
220220
#}
221-
{{- block('form_rows') }}
221+
{{- block('form_rows') -}}
222222
{%- endblock repeated_row %}
223223

224224
{% block form_row -%}
@@ -253,17 +253,17 @@
253253
{% set form_method = method %}
254254
{%- else -%}
255255
{% set form_method = "POST" %}
256-
{% endif %}
256+
{%- endif -%}
257257
<form name="{{ form.vars.name }}" method="{{ form_method|lower }}" action="{{ action }}"{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %}>
258-
{% if form_method != method %}
258+
{%- if form_method != method -%}
259259
<input type="hidden" name="_method" value="{{ method }}" />
260-
{%- endif %}
260+
{%- endif -%}
261261
{%- endblock form_start %}
262262

263263
{% block form_end -%}
264-
{% if not render_rest is defined or render_rest -%}
265-
{{ form_rest(form) }}
266-
{%- endif -%}
264+
{% if not render_rest is defined or render_rest %}
265+
{{- form_rest(form) -}}
266+
{% endif -%}
267267
</form>
268268
{%- endblock form_end %}
269269

@@ -283,18 +283,18 @@
283283

284284
{% block form_rest -%}
285285
{% for child in form -%}
286-
{% if not child.rendered -%}
287-
{{ form_row(child) }}
288-
{%- endif %}
286+
{% if not child.rendered %}
287+
{{- form_row(child) -}}
288+
{% endif %}
289289
{%- endfor %}
290-
{%- endblock form_rest %}
290+
{% endblock form_rest %}
291291

292292
{# Support #}
293293

294294
{% block form_rows -%}
295-
{% for child in form -%}
296-
{{ form_row(child) }}
297-
{%- endfor %}
295+
{% for child in form %}
296+
{{- form_row(child) -}}
297+
{% endfor %}
298298
{%- endblock form_rows %}
299299

300300
{% block widget_attributes -%}

src/Symfony/Bridge/Twig/Resources/views/Form/form_table_layout.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
{{- form_widget(form) -}}
2020
</td>
2121
</tr>
22-
{%- endblock button_row %}
22+
{% endblock button_row %}
2323

2424
{% block hidden_row -%}
2525
<tr style="display: none">
2626
<td colspan="2">
2727
{{- form_widget(form) -}}
2828
</td>
2929
</tr>
30-
{%- endblock hidden_row %}
30+
{% endblock hidden_row %}
3131

3232
{% block form_widget_compound -%}
3333
<table {{ block('widget_container_attributes') }}>
34-
{% if form.parent is empty and errors|length > 0 -%}
34+
{%- if form.parent is empty and errors|length > 0 -%}
3535
<tr>
3636
<td colspan="2">
3737
{{- form_errors(form) -}}

src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -756,16 +756,4 @@ public function testWidgetContainerAttributeNameRepeatedIfTrue()
756756
// foo="foo"
757757
$this->assertContains('<div id="form" foo="foo">', $html);
758758
}
759-
760-
public function testWidgetContainerAttributeHiddenIfFalse()
761-
{
762-
$form = $this->factory->createNamed('form', 'form', null, array(
763-
'attr' => array('foo' => false),
764-
));
765-
766-
$html = $this->renderWidget($form->createView());
767-
768-
// no foo
769-
$this->assertContains('<div id="form">', $html);
770-
}
771759
}

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,8 +1931,7 @@ public function testWidgetAttributeHiddenIfFalse()
19311931

19321932
$html = $this->renderWidget($form->createView());
19331933

1934-
// no foo
1935-
$this->assertSame('<input type="text" id="text" name="text" required="required" value="value" />', $html);
1934+
$this->assertNotContains('foo="', $html);
19361935
}
19371936

19381937
public function testButtonAttributes()
@@ -1968,7 +1967,38 @@ public function testButtonAttributeHiddenIfFalse()
19681967

19691968
$html = $this->renderWidget($form->createView());
19701969

1970+
$this->assertNotContains('foo="', $html);
1971+
}
1972+
1973+
public function testTextareaWithWhitespaceOnlyContentRetainsValue()
1974+
{
1975+
$form = $this->factory->createNamed('textarea', 'textarea', ' ');
1976+
1977+
$html = $this->renderWidget($form->createView());
1978+
1979+
$this->assertContains('> </textarea>', $html);
1980+
}
1981+
1982+
public function testTextareaWithWhitespaceOnlyContentRetainsValueWhenRenderingForm()
1983+
{
1984+
$form = $this->factory->createBuilder('form', array('textarea' => ' '))
1985+
->add('textarea', 'textarea')
1986+
->getForm();
1987+
1988+
$html = $this->renderForm($form->createView());
1989+
1990+
$this->assertContains('> </textarea>', $html);
1991+
}
1992+
1993+
public function testWidgetContainerAttributeHiddenIfFalse()
1994+
{
1995+
$form = $this->factory->createNamed('form', 'form', null, array(
1996+
'attr' => array('foo' => false),
1997+
));
1998+
1999+
$html = $this->renderWidget($form->createView());
2000+
19712001
// no foo
1972-
$this->assertSame('<button type="button" id="button" name="button">[trans]Button[/trans]</button>', $html);
2002+
$this->assertNotContains('foo="', $html);
19732003
}
19742004
}

src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -533,16 +533,4 @@ public function testWidgetContainerAttributeNameRepeatedIfTrue()
533533
// foo="foo"
534534
$this->assertContains('<table id="form" foo="foo">', $html);
535535
}
536-
537-
public function testWidgetContainerAttributeHiddenIfFalse()
538-
{
539-
$form = $this->factory->createNamed('form', 'form', null, array(
540-
'attr' => array('foo' => false),
541-
));
542-
543-
$html = $this->renderWidget($form->createView());
544-
545-
// no foo
546-
$this->assertContains('<table id="form">', $html);
547-
}
548536
}

0 commit comments

Comments
 (0)