I want to create in Laravel something like this structure:

  1. The main template file: views / index.blade.php, which contains within it sections head (meta tags), content and sections for displaying modules (widgets) (for example, @yield('content') .
  2. Template files for each of the pages: views / pages / ..., where each of the pages describes the sections specified in the "index.blade.php".
  3. Widget files: views / widgets / ..., which describes the markup of all widgets.

How is this implemented on Laravel? For example, the controller displays the mapping for a specific page as follows:

 return view('index'); 

Thus, I will display the common template file "index.blade.php". How to make so that, for example, a common template file is displayed on each page, and the text inside the head and content areas is generated by files inside the "pages" folder?

And the second question: is it correct in this case to connect widgets (modules) using the @include directive inside the file of a specific page (views / pages)? Something like that:

 @section('widgets') @include('widgets.miniprofile') @stop 

The widgets section itself would be described inside the main "index.blade.php" (yield).

    2 answers 2

    For a global template, use @extend https://laravel.com/docs/5.1/blade#extending-a-layout

    Make a master file from your index.blade.php and the others just expand it. Those. each new file in "pages" will consist of blocks

     @section('head') ... @endsection @section('content') ... @endsection 

    Everything else will be described in the main master file.

    In general, everything in the above documentation is well written, also in laravel 5.2 you can make artisan make: auth, this command will generate an authorization template and the main page, you can see how the structure of the views is made there.

    Regarding the inclusion of widgets, yes, in general, all right. You can embed them inside the view from the "pages", if they are different for all pages, but you can immediately inside the main master file.

    I'm just starting to figure it out, but I would do it something like this: I added $data to the controller. Filled it with the right data. In the 'widgets' key, add the data to the key that is to be transferred to the layout.

    For example: $data['widgets']['menu'] = []

    In the layout, we check the key, if it is transferred, we @foreach or through @foreach run over the variable data from the widgets and enable the corresponding part of the template by the name of the key, passing in the data of this array.

    I think this is the most optimal implementation, I will try. He himself now wondered, and answered himself.

    Now I use the following template structure:

     views/templates/<template-name>.blade.php views/layouts/<layout-name>.blade.php views/particles/<widgets>/<name>.blade.php 

    From the controller, we call the desired layout, and the template is expanded in the layout, and the method of calling widgets above is also indicated there.

    • I also found this article, if the functionality is not wrapped up, then it is possible to collect data for views in the controller, and also simply include them in the layout of habrahabr.ru/post/222453 p.6 - quadrogod