laravel-mobile-detect icon indicating copy to clipboard operation
laravel-mobile-detect copied to clipboard

Blades extending layout

Open EggIt opened this issue 5 years ago • 1 comments

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?

EggIt avatar Aug 04 '19 01:08 EggIt

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.

barnabaskecskes avatar Aug 05 '19 20:08 barnabaskecskes