2

I have the error "A template that extends another one cannot have a body in DFSiteBundle:Accueil:index.html.twig ", but I have no html outside the blocks.

Smallest code that replicate the problem :

This doesn't work:

{% extends "::layout.html.twig" %}

{% stylesheets 'bundles/DFSite/css/*' filter='cssrewrite' %}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

{% block content %}
    Test
{% endblock %}

This work:

{% extends "::layout.html.twig" %}

{% block content %}
    Test
{% endblock %}

With layout.html.twig :

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    {% stylesheets %}{% endstylesheets %}
    <title>{% block title %}The title{% endblock %}</title>
</head>
<body>

{% block content %}
{% endblock %}

</body>

And if I add directly this

{% stylesheets 'bundles/DFSite/css/*' filter='cssrewrite' %}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

instead of the empty %stylesheets% in layout.html.twig, it works

I'm confused...

Tryss
  • 303
  • 2
  • 8

1 Answers1

6

It's easy to understand the error here: when you extend a template you are forced to use only defined blocks of parent or brand-new blocks.

Looking to your layout.html.twig it's quite easy to notice that for {% stylesheets %} you are not defining any block. In your child template this will cause the error I was talking about.

A working solution should be this

1) Modify layout.html.twig

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    {% block stylesheets %}{% endblock %}
    <title>{% block title %}The title{% endblock %}</title>
</head>
<body>

{% block content %}
{% endblock %}

</body>

2) Modify the file that extends previous template

{% extends "::layout.html.twig" %}

{% block stylesheets %}
    {% stylesheets 'bundles/DFSite/css/*' filter='cssrewrite' %}
        <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}

{% block content %}
    Test
{% endblock %}
DonCallisto
  • 27,046
  • 8
  • 62
  • 88
  • @Tryss: it's a kind of "special" block, but not like that kind of block that keep you away from that error. Of course I will not teach you about stylesheets "special" block as I suppose that you perfectly know its use ;) – DonCallisto Mar 19 '15 at 08:41