Tag Archives: extends

Fix ‘A template that extends another one cannot have a body in …’ in Twig with Symfony2

This may be a confusing error at first but it’s quite self explanatory. You can’t have any HTML text in a Twig template that extends another, not even comments.

Twig_Error_Syntax

So for example this would raise the error:

<!-- src/Acme/DemoBundle/Resources/views/Default/index.html.twig -->
{% extends 'AcmeDemoBundle::layout.html.twig' %}

{% block stylesheets %}
  {{ parent() }}
  <link rel="stylesheet" href="{{ asset('bundles/demo/css/base.css') }}" type="text/css" media="all" />
{% endblock %}

The solution: delete the HTML comment in the first line or use Twig comments:

{# src/Acme/DemoBundle/Resources/views/Default/index.html.twig #}
{% extends 'AcmeDemoBundle::layout.html.twig' %}

{% block stylesheets %}
  {{ parent() }}
  <link rel="stylesheet" href="{{ asset('bundles/demo/css/base.css') }}" type="text/css" media="all" />
{% endblock %}