8000 Merge remote branch 'vicb/twig-theme-inheritance' · 77web/symfony@0a7ce63 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a7ce63

Browse files
committed
Merge remote branch 'vicb/twig-theme-inheritance'
* vicb/twig-theme-inheritance: [Form] Further tweaks of the twig theme inheritance [Form] Fix twig theme inherit 10000 ance
2 parents 19a8ece + 5060702 commit 0a7ce63

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ protected function render(FormView $view, $section, array $variables = array())
249249
/**
250250
* Returns the templates used by the view.
251251
*
252-
* templates are looked for in the following resources:
252+
* Templates are looked for in the resources in the following order:
253253
* * resources from the themes (and its parents)
254+
* * resources from the themes of parent views (up to the root view)
254255
* * default resources
255256
*
256257
* @param FormView $view The view
@@ -260,29 +261,25 @@ protected function render(FormView $view, $section, array $variables = array())
260261
protected function getTemplates(FormView $view)
261262
{
262263
if (!$this->templates->contains($view)) {
263-
// defaults
264-
$all = $this->resources;
265-
266-
// themes
264+
$resources = array();
267265
$parent = $view;
268266
do {
269267
if (isset($this->themes[$parent])) {
270-
$all = array_merge($all, $this->themes[$parent]);
268+
$resources = array_merge($this->themes[$parent], $resources);
271269
}
272270
} while ($parent = $parent->getParent());
273271

272+
$resources = array_merge($this->resources, $resources);
273+
274274
$templates = array();
275-
foreach ($all as $resource) {
275+
foreach ($resources as $resource) {
276276
if (!$resource instanceof \Twig_Template) {
277277
$resource = $this->environment->loadTemplate($resource);
278278
}
279279

280-
$blocks = array();
281280
foreach ($this->getBlockNames($resource) as $name) {
282-
$blocks[$name] = $resource;
281+
$templates[$name] = $resource;
283282
}
284-
285-
$templates = array_replace($templates, $blocks);
286283
}
287284

288285
$this->templates->attach($view, $templates);

tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionDivLayoutTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,48 @@ protected function setUp()
4848
$this->extension->initRuntime($environment);
4949
}
5050

51+
public function testThemeInheritance()
52+
{
53+
$child = $this->factory->createNamedBuilder('form', 'child')
54+
->add('field', 'text')
55+
->getForm();
56+
57+
$view = $this->factory->createNamedBuilder('form', 'parent')
58+
->add('field', 'text')
59+
->getForm()
60+
->add($child)
61+
->createView()
62+
;
63+
64+
$this->extension->setTheme($view, array('parent_label.html.twig'));
65+
$this->extension->setTheme($view['child'], array('child_label.html.twig'));
66+
67+
$this->assertWidgetMatchesXpath($view, array(),
68+
'/div
69+
[
70+
./input[@type="hidden"]
71+
/following-sibling::div
72+
[
73+
./label[.="parent"]
74+
/following-sibling::input[@type="text"]
75+
]
76+
/following-sibling::div
77+
[
78+
./label
79+
/following-sibling::div
80+
[
81+
./div
82+
[
83+
./label[.="child"]
84+
/following-sibling::input[@type="text"]
85+
]
86+
]
87+
]
88+
]
89+
'
90+
);
91+
}
92+
5193
protected function renderEnctype(FormView $view)
5294
{
5395
return (string)$this->extension->renderEnctype($view);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% block field_label %}
2+
<label>child</label>
3+
{% endblock field_label %}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% block field_label %}
2+
<label>parent</label>
3+
{% endblock field_label %}

0 commit comments

Comments
 (0)
0