Symfony2 Twig including header with assets not wor

2019-07-21 15:21发布

问题:

I'm trying to include my header into another template, not extending, like this

{% include '@Foo:Bar:_header.html.twig' %}

That header, contains this

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>{{ site.name }}</title>

    {% block stylesheets %}
        {% stylesheets
            "@Foo/Resources/public/app.css"
        %}
            <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock stylesheets %}
</head>
<body>

Nothing unusual, just some css file with assetic.

This behaviour gives throws me this exception

An exception has been thrown during the compilation of a template ("The template does not support the "bundle" parameter.") in "@Foo/bar/_header.html.twig".

huh?

If instead of including it {% include %} I extend it {% extends %} everything works perfectly.

Why I want to include instead of extend? I want to create new blocks, and include other stuff etc.

This problem is ok? Is this the way it's supossed to work?

回答1:

Silly answer, silly problem.

Fix was removing @ from include, I don't know why I used it.

{% include '@Foo:Bar:_header.html.twig' %} not working

{% include 'Foo:Bar:_header.html.twig' %} working



回答2:

See issue: https://github.com/symfony/AsseticBundle/issues/331

It states that to fix this issue you should update AsseticBundle to "master-dev" branch (this issue has been fixed few days ago, but not yet merged in a stable release of the Bundle)