Missing layout? Make sure you define block for content

I’ve had some troubles with a page that refused to be wrapped in a layout. It turned out I accidentally erased {block #content} in my presenter template. Read on for the background logic explanation.

Here’s my @layout.latte:

  1. <!DOCTYPE …
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6.  {include #content}
  7. </body>
  8. </html>

Note the request to include block #content. I thought that this block would get defined automatically via the inner workings of Nette framework. It turned out that my presenter template needs to define the block specifically.

The surprise that comes when you don’t do so is not that you get a layout without the guts part but vice versa – you get the inner template without the wrapping layout. No error, nice and quiet.

The way I discovered the culprit was that I requested the layout to be added specifically with {layout '../@layout.latte'}.
And bang! I got an InvalidStateException about a missing block.

So, make sure you put {block #content} at the top of your presenter template:

  1. {block #content} {* required for a layout to be added *}
  3. <h1>Administration</h1>
  5. <div>…

Guess what happens if you don’t put it at the very top ;-)


Leave a Reply