Te-cho / compile-blades

For Laravel 5.* views, where i've noticed that if you nest too much views, a performance drop happens. so i built this console command based package, that flatten the view into one file for production performance improvement.
https://packagist.org/packages/te-cho/compile-blades
MIT License
64 stars 23 forks source link

Issues with compiling layouts or page #12

Open thewebartisan7 opened 4 years ago

thewebartisan7 commented 4 years ago

I have tested this package for compile my layout file which include too much things.

However the result was not as expected.

But commenting method implodeLayout() worked fine, this:


    private function compile($viewPath)
    {
        $blade = file_get_contents($viewPath);
        //$this->implodeLayout($blade);//A1 @inprogress
        $this->implodeIncludes($blade);//A2 @pending

        return $blade;
    }

Without this the main yield('content') is replaced by:

{{--yield didnt have alternative--}}

Is not designed for works also with layouts?

If I try to compile a view that extends a layout, also not works, this is the result (most part are removed):


{{-- Extend layout was here --}}

@section('content')
    <div>
This is content
    </div>
@endsection
 <!DOCTYPE html>
<html>
<head>
    <?php extract([]); ?>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{csrf_token()}}">
<title>{{config('app.name')}}@yield('head-title')</title>
@stack('styles')
</head>
<body>
    <main class="app">
       {{--yield didnt have alternative--}}  // ... HERE SHOULD BE yield content
    </main>
@section('scripts')
    <?php extract([]); ?>
@stack('scripts')
<?php extract([]); ?>

// ... removed to shorten issue

@show
</body>
</html>

As you can see yield('content') is removed and the view compiled is on top and not inside content.

Maybe this works only with partials views?

Then I wonder why is added this php code for each included file:

<?php extract([]); ?>

Thanks

thewebartisan7 commented 4 years ago

I check a bit inside code and I understand now why not works compiling page that extend layout.

this code:

 $blade = preg_replace("/@section(?s).*?stop/si", "{{-- section was here --}}", $blade);

Doesn't work when @section is closed using @endsection, but must be @stop

Now remain only issue with compiling layouts.

Let me know if there is a way, I will continue to check in code maybe I understand.

Thanks

thewebartisan7 commented 4 years ago

I find a workaround for compile also layouts by keeping yields(). It was easy, but not sure if this can make some issue. I tested and seem works fine. But maybe I am introducing some issue. Please let me know what do you think.


    private function implodeLayout(&$blade)
    {
        $sections = $this->seperateSections($blade);//B1 @done
        $this->replaceLayout($blade);//B2 @done

        // Just commented this:
        //$this->replaceSections($blade, $sections);//B3 @inprogress
    }

If this works, could be add a new options for keep yield so that works also in this case.

What do you think?

I also see what it's used the extract() method, to extract variable passed to partials view.