Question

I want to create a reusable block in laravel blade template

I write pseudo code below, want to know how I can implement the scope

// app/views/layouts/code.blade.php
// the block I want to reuse
<pre>
    <code>
        // fill the content here
    </code>
</pre>

// app/views/myview.blade.php

@extends('layouts.main') // set header, body, and footer block

@section ('content')
    @reuse_section('layouts.code')
       .......my code line 1
       .......my code line 2
    @endsection

    @reuse_section('layouts.code')
        .......my code line 1
        .......my code line 2
        .......my code line 3
    @endsection
 @endsection
Was it helpful?

Solution

you're best off creating a new View::make() call within your blade template.

// myview.blade.php
<?= View::make('partials.code', [
    'code' => 'my dynamic code content'
]); ?>

and your code.blade.php file takes the parameters just like:

// partials/code.blade.php
<pre>
    <code>
        {{ $code }}
    </code>
</pre>

OTHER TIPS

You can include view/block in template too. And pass variables, as well.

@extends('layouts.main') // set header, body, and footer block

@section ('content')
    @include('layouts.code', ['code'=>'some code'])
    @include('layouts.code', ['code'=>'some other code'])
 @endsection

 // layouts.code.blade.php
 <code>
    {{ $code }}
 </code>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top