Including a Blade Template within another Template


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.


Use the @include statement.

    <h1>Page One</h1>
    <p>bla, bla, bla</p>

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:

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

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

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


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