Skip to content

Loading…

Assets defined in overridden blocks of embedded Twig templates are not loaded #585

Merged
merged 2 commits into from

2 participants

@bangpound

Using the Twig embed tag, I am including a template and extending/overriding blocks within it. This is the code in question:

{% block footer %}
    {% embed 'Bundle::footer.html.twig' %}
        {% block sponsors %}
            {{ parent() }}
            <li>
                <a href="http://website.org">
                    {% image '@Bundle/Resources/public/images/logo.png' %}
                    <img src="{{ asset_url }}" alt="Organization Logo" />
                    {% endimage %}
                </a>
            </li>
        {% endblock %}
    {% endembed %}
{% endblock %}

However, the blocks inside the embed tag are never scanned for instances of AsseticNode, because the embedded template is not a child but rather it's an attribute on the node.

@stof
Collaborator

Please add a test for this

@bangpound

I've added a test.

The test proves that the asset in the embed.twig is identified when the embedded.twig template is embedded and its assets block is overridden.

@stof stof merged commit c501da7 into kriswallsmith:master

1 check passed

Details default The Travis CI build passed
@bangpound bangpound deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 src/Assetic/Extension/Twig/TwigFormulaLoader.php
@@ -94,6 +94,12 @@ private function loadNode(\Twig_Node $node)
}
}
+ if ($node->hasAttribute('embedded_templates')) {
+ foreach ($node->getAttribute('embedded_templates') as $child) {
+ $formulae += $this->loadNode($child);
+ }
+ }
+
return $formulae;
}
}
View
25 tests/Assetic/Test/Extension/Twig/TwigFormulaLoaderTest.php
@@ -105,4 +105,29 @@ public function testUnclosedTag()
$formulae = $this->loader->load($resource);
$this->assertEquals(array(), $formulae);
}
+
+ public function testEmbeddedTemplate()
+ {
+ $expected = array(
+ 'image' => array(
+ array('images/foo.png'),
+ array(),
+ array(
+ 'name' => 'image',
+ 'debug' => true,
+ 'vars' => array(),
+ 'output' => 'images/foo.png',
+ 'combine' => false,
+ ),
+ ),
+ );
+
+ $resource = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
+ $resource->expects($this->once())
+ ->method('getContent')
+ ->will($this->returnValue(file_get_contents(__DIR__.'/templates/embed.twig')));
+
+ $formulae = $this->loader->load($resource);
+ $this->assertEquals($expected, $formulae);
+ }
}
View
7 tests/Assetic/Test/Extension/Twig/templates/embed.twig
@@ -0,0 +1,7 @@
+<assets>
+{% embed 'embedded.twig' %}
+ {% block assets %}
+ {% image 'images/foo.png' output='images/foo.png' name='image' debug=true combine=false %}<image url="{{ asset_url }}" />{% endimage %}
+ {% endblock %}
+{% endembed %}
+</assets>
View
3 tests/Assetic/Test/Extension/Twig/templates/embedded.twig
@@ -0,0 +1,3 @@
+{% block assets %}
+ {% image 'images/bar.png' output='images/bar.png' name='image' debug=true combine=false %}<image url="{{ asset_url }}" />{% endimage %}
+{% endblock %}
Something went wrong with that request. Please try again.