laravel-mobile-detect
laravel-mobile-detect copied to clipboard
Blades extending layout
Hi,
I got a blade file as follows:
@desktop
@extends('layouts.app')
@section('content')
<h1>DESKTOP VERSION</h1>
@endsection
@enddesktop
@handheld
<h1>MOBILE VERSION</h1>
@endhandheld
The h1 tags are working as expected, on desktop it shows DESKTOP VERSION and on mobile it shows MOBILE VERSION.
But the same layout layouts.app is being used by both clients.
I expected the layout to be used by desktop only, is this an issue or did i miss something?
Hey @EggIt thanks for the issue. I think this happens because how @extends() work within the framework. I tried above and got the same results.
I don't know what your use-case is, but what I would probably do is to create separate layout files (eg. layouts.desktop and layouts.handheld). You can then use
View::share('layout', app('mobile-detect')->isMobile() ? 'layouts.handheld' : 'layouts.desktop');
in your AppServiceProvider::boot() for example, then you can just do @extends($layout) in your blade files and it would just pick the right one every time.
Note that
app('mobile-detect')will expose the underlying third party implementation and the language is a bit different. To learn more, check the directive implementations and see how the one you need is composed.