Where to Place Blade::Extend

Where to place Blade::extend

There's no requirement telling you where you should put the code, you could even put it in your routes.php (which is a bit messy of course). You only have to make sure that it's loaded when laravel processes a page view.

In this case, creating a new file blade_extensions.php somewhere and including it in start/global.php might be a good solution.

PS: Be sure to clear out your compiled views, as Blade only recompiles the views if it detects a change, so if you've just plonked in this code it won't work until you clear out the views.

Laravel @extends and @include

@include is just like a basic PHP include, it includes a "partial" view into your view.

@extends lets you "extend" a template, which defines its own sections etc. A template that you can extend will define its own sections using @yield, which you can then put your own stuff into in your view file.

Example:

template.blade.php

<html>
<body>
@yield('header')
@yield('content')
@yield('footer')
</body>
</html>

view-one.blade.php

@extends('template')

@section('header')
View one's header
@endsection

@section('content')
View one's content
@endsection

@section('footer')
View one's footer
@endsection

Which will result in:

<html>
<body>
View one's header
View one's content
View one's footer
</body>
</html>

Now you could create another view which extends the same template, but provides its own sections.

Another benefit to using @extend is inheritance. You could provide a base template, and then another child template that extends that one which subsequently yields it's own sections. You can then extend that child template.

How to extend Laravel Blade functionality and add 'break' and 'continue' support

Did you clear cache/compiled view files after adding the provided piece of your code to routes.php ? If not, try doing so as Blade will recompile the views only if changes are detected in them. So if you didn't clear the compiled views after adding the code, nothing changed in the rendered html.

If it's not the case, try using plain old regexp instead of the Blade::createMatcher, this nice definition will provide you with both continue and break support in a oneliner.

Blade::extend(function($value)
{
return preg_replace('/(\s*)@(break|continue)(\s*)/', '$1<?php $2; ?>$3', $value);
});

It should work even if placed in routes.php, although it's better to put it in a separate file (for example blade.php and include it in global.php). Anyway it must be loaded prior to the view being processed.

Conditional extends in Blade

in the master layout:

   @if(!Request::ajax())

//the master layout with @yield('content'). i.e. your current layout

@else

@yield('content')

@endif

Extending blade template for opening and closing tags

Create a new file blade_extensions.php in your app/ folder. Then inside that file put the following:

<?php

Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createMatcher('foo');

return preg_replace($pattern, '$1<?php if ($2 == "foo") : ?>', $view);
});

Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createPlainMatcher('endfoo');

return preg_replace($pattern, '$1<?php endif; ?>', $view);
});

Then in your app/start/global.php at the bottom after require app_path().'/filters.php'; add:

require app_path().'/blade_extensions.php';

Then in your view you can write the following:

@foo ($var)
bar
@endfoo

I don't know if that's exactly the control structure you're looking for, but that matches the check for $var == 'foo'

How to EXTEND a blade template only if it exists?

Try doing it like this:

@extends( $somecondition == true ? 'one' : 'two')

In Laravel blade @extends layout with condition how to pass the data

Your syntax is wrong. Try this instead:

@extends( (condition) ? 'layouts.full' : 'layouts.full2', [ 'data' => ['var' => 'key'] ])

If you need different data depending on the layout used, you should use IF statements instead, to avoid testing two times :

@if(condition)
@extends('layouts.full', [ 'data' => ['var' => 'key'] ])
@else
@extends('layouts.full2', [ 'data2' => ['var2' => 'key2'] ])
@endif

However, this doesn't seem to work, so you should place this logic in the controller.

if(App::environment() === 'local') {
$view = 'layouts.full';
$data = ['class' => 'blue'];
} else {
$view = 'layouts.full2';
$data = ['class' => 'red'];
}

return view('viewname', compact('view', 'data'));

And in the view :

@extends( $view, $data )


Related Topics



Leave a reply



Submit