Basically I want to @extend a blade template only if it exists, and if not @extend a different template. There are a few stack overflow answers concerning using @if @endif blocks, but that only works if you are @including a file, not @extending. Ideally something like this, but it doesn't work:
@if(some_condition == true)
@extends('one')
@else
@extends('two')
@endif
If the only way is to use Blade directives, could you please provide an example? Thank you!
Try doing it like this:
@extends( $somecondition == true ? 'one' : 'two')
@if( file_exists('path to file one'))
@extends('one')
@else
@extends('two')
@endif
you can use view:exists
@if(View::exists('path.to.view.one'))
@extends('one')
@else
@extends('two')
@endif
you can use the conditional to define the name of the view you want to load and then simply extend it, for example:
@php
$view = '';
if (some_condition == true) {
$view = 'one';
} else {
$view = 'two';
}
@endphp
...
@extends($view)
more info
You can use View::exists
@if (View::exists('one'))
@extends('one')
@else
@extends('two')
@endif
or
file_exists()
and to get the path use resource_path()
this,
@if (file_exists(resource_path('views/one.blade.php')))
@extends('one')
@else
@extends('two')
@endif
You can try this, in my case that's working.. See the docs in Laravel - https://laravel.com/docs/5.5/views