Including a Blade Template within another Template

Problem

You want to include one Blade template within another.

You combined some common HTML code into a single template and want to pull in the template within another template.

Solution

Use the @include statement.

<html>
<body>
    @include('common-header')
    <h1>Page One</h1>
    <p>bla, bla, bla</p>
</body>
</html>

When the above view is created it will look for views/common-header.php or views/common-header.blade.php and replace @include('common-header') with the contents of the file.

You can also specify variables

By default, any included templates inherit any variables the including template had available, but you can specify new ones.

Let's say you have views/partials/item-display.blade.php that looks like:

<div>
    Name: {{ $item->name }}<br>
    Description: {{{ $item->description }}}
</div>

Then you could have a template include this "partial", passing the $item variable.

<html>
<body>
  <h1>All the items:</h1>
  @foreach ($items as $foo)
    @include('partials.item-display', array('item' => $foo))
  @endforeach
</body>

Discussion

The @include() line must not span multiple lines.

Code like below will not operate correctly.

@include('my-partial-name', array(
    'value1' => 'abc',
    'value2' => $some_var,
    'value3' => date('Y-m-d'),
))
comments powered by Disqus