Duda View Composer and View Creator Laravel 5

0

Using laravel 5.2 I have two views: principal.blade :

<!DOCTYPE html>
<html>
    <head>
         <title>@yield('meta_title', $meta_title)</title>
    </head>
    <body>
        <div id="main">
            @yield('content')
        </div>
    </body>
</html>

and secundaria.blade :

@extends('principal')

@section('content')
    <p>Lorem ipsum </p>
@endsection

In the main I have what comes to be the template and in the secondary the content.

I created a "CreatorServiceProvider" where I call:

public function boot()
{
    view()->creator('principal', 'App\Http\ViewCreators\MetaCreator');
}

And in MetaCreator I call:

public function create($view)
{
    $view->with([
        'meta_title' => getTheTitle(),
    ]);
}

so that each time the "main" view is created it has some predefined values (title, description, etc ...), these values fill them according to the url that it has at that moment. I use "ViewCreator" because I want to be able to overwrite these values at some point in the call if necessary.

The problem does not come at the time of sharing the variable meta_title but at the time of overwriting it. The variable meta_title does not overwrite me with the following code:

return view('secundaria')->with('meta_title', 'Titulo 2');

I can only overwrite it if I call directly:

return view('principal')->with('meta_title', 'Titulo 2');

The problem is that I in the controller need to call the secondary view, which extends the main one and overwrite the value meta_title , and not the main view.

Right now this problem solves it by passing the value of the meta_title to secondary view and this view by calling @section() I give the value to meta_title :

Controller Line where I call the secondary view:

return view('secundaria', ['meta_title' => 'Titulo 2']);

And the secondary view:

@extends('principal')

@section('meta_title', $meta_title)

@section('content')
    <p>Lorem ipsum </p>
@endsection

But I would like to know if you can do this directly from the controller without having to go through the view to change the value:

return view('secundaria')->with('meta_title', 'Titulo 2');

Can it be done like this? Is it well planned or is there another better way to raise it?

Thank you.

    
asked by Pablone 31.01.2017 в 14:49
source

2 answers

2

@yield receives two parameters

principal.blade.php

<!DOCTYPE html>
<html>
  <head>
     <title>@yield('titulo', 'Valor por defecto')</title>
  </head>
  <body>
    <div>
        @yield('contenido')
    </div>
  </body>
</html>

And now in the secondary view we pass the parameter:

@section('titulo', $titulo )  //

@section('contenido')
@endsection
    
answered by 31.01.2017 в 19:05
0

Unless you do not understand the problem, I do not see the need for a ViewCreator, you can simply use another yield to overwrite the default value you assign to meta_title :

<!DOCTYPE html>
    <html>
    <head>
         <title>@yield('meta_title', 'Título por defecto')</title>
    </head>
<body>
    <div id="main">
        @yield('content')
    </div>
</body>

secondary view:

@extends('principal')

@section('main_title')
    Mi título personalizado o {{ $nuevo_titulo }}
@endsection

@section('content')
    <p>Lorem ipsum </p>
@endsection

The values of the titles can be replaced by variables.

    
answered by 31.01.2017 в 16:08