Please update your post with a better title. "view" isn't useful.
@yield
Hello, I have implemented a simple code just to try using "@yield", that is the code:
- The parent file:
- The child file: @extends ( 'test')
@section ( 'containertest') Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore dolore magna aliqua and. Ut enim ad minim veniam, quis nostrud laboris nisi ut exercitation ullamco aliquip ex ea commodo consequat. Duis aute Irure in dolor velit esse reprehenderit in voluptate cillum dolore nulla had fugiat pariatur. Excepteur sint occaecat cupidatat not proident, sunt in culpa who officiated deserunt abated anim id est Laborum. @endsection
==> The contents of the section 'containertest' does not appear
When I test another code I found that "@yield ( 'containertest')" is ignored.
Where is the problem in this case ?
What view are you loading?
The parent template 'test': .... return view('test');
yes
yes the two files have the extension ".blade.php"
You render the child for the view and then the child defers to the parent or master layout.
The master layout is then rendered until it comes to a yield, at which point it includes the content from the child.
suppose you have master.blade.php;
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>
and then you have an about us page at about.blade.php
@extends('master')
@section('title')
About Us
@endsection
@section('content')
<div>my about us page here</div>
@endsection
Then in the controller
return view('about'); //not master
I implemented your example, the error that I had is:
ErrorException in C: \ wamp \ www \ Laravel \ vendor \ Laravel \ framework \ src \ Illuminate \ View \ FileViewFinder.php line 137: View [app] not found. (View: C: \ wamp \ www \ Laravel \ resources \ views \ about.blade.php)
I get an error :
ErrorException in C: \ wamp \ www \ Laravel \ vendor \ Laravel \ framework \ src \ Illuminate \ View \ FileViewFinder.php line 137: View [app] not found. (View: C: \ wamp \ www \ Laravel \ resources \ views \ about.blade.php)
It says that view file dont exist ... do you create app.blade.php?
No, the "view" folder contains:
- "Welcome.blade.php" file,
- the two folders : "errors" and "vendor".
- the parent template,
- the child template ,
I did not create any other template
Can you post your view files which generate error (about.blade.php)
My example is to point you in the right direction. Not just copy it literally.
Just make sure you return view('child') and not the parent
If you want more help, I suggest you post your code instead of 'parent and 'child'
Hello, I apologize for the delay, the code is:
- routes.php :
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/1/{name}', 'Homecontroller@test');
- Homecontroller.php :
<?php
namespace App\Http\Controllers;
class Homecontroller extends Controller
{
public function index()
{
return view('home');
}
public function test($name)
{
return view('testc');
}
}
- views/testc.blade.php :
@extends('layouts.testp')
@section('content')
<div>my about us page here</div>
@endsection
- views/layouts/testp.blade.php :
@extends('layouts.testp')
@section('content')
<div>my about us page here</div>
@endsection
Thank you, in advance for your valuable assistance.
Route.php
Route::get('/1/{name}', 'uses' => 'Homecontroller@test');
Homecontroller.php
namespace App\Http\Controllers;
class Homecontroller extends Controller {
public function index() {
return view('home');
}
public function test($name) {
return view('testc', $name);
}
}
views/testc.blade.php :
@extends('layouts.testp')
@section('content')
<div>{{ $name about us page here }}</div>
@stop
views/layouts/testp.blade.php :
@section('content')
Yes, use "@stop" rather than @endsection
@stop is just shorthand for @endsection. They should both be interchangeable.
BladeCompiler.php:
/**
* Compile the end-section statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndsection($expression)
{
return '<?php $__env->stopSection(); ?>';
}
// ...
/**
* Compile the stop statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileStop($expression)
{
return '<?php $__env->stopSection(); ?>';
}
Are these guys twins, or what? :)
Thank you for your reply, but I get an error : " FatalErrorException in routes.php line 18: syntax error, unexpected '=>' (T_DOUBLE_ARROW) ".
In testp.blade.php file, where can I write
@yield (content)
??
I am thefuzzy0ne - psychic extraordinaire! Nah! Just kidding. That's why I have absolutely no idea what in your routes.php.
@thefuzzy0ne, Route.php
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/1/{name}', 'uses' => 'Homecontroller@test');
Homecontroller.php
namespace App\Http\Controllers;
class Homecontroller extends Controller {
public function index() {
return view('home');
}
public function test($name) {
return view('testc', $name);
}
}
views/testc.blade.php :
@extends('layouts.testp')
@section('content')
<div>{{ $name about us page here }}</div>
@stop
views/layouts/testp.blade.php :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
@yield('content')
</body>
</html>
There's your problem!
Route::get('/1/{name}', 'uses' => 'Homecontroller@test');
should be:
Route::get('/1/{name}', ['uses' => 'Homecontroller@test']);
@thefuzzy0ne another error :
" ErrorException in Factory.php line 167: array_merge(): Argument #2 is not an array "
OK, here's that problem! :-)
class Homecontroller extends Controller {
// ...
public function test($name) {
return view('testc', ['name' => $name]); // Expects an array as the second parameter
}
}
Or neater still:
class Homecontroller extends Controller {
// ...
public function test($name) {
return view('testc', compact('name')); // Expects an array as the second parameter
}
}
One more thing, by convention, generally it should be HomeController not Homecontroller. Either will work, but I think one is more readable.
Thank you, but I get another error (after rectification) :
" ErrorException in FileViewFinder.php line 137: View [app] not found. (View: C:\wamp64\www\Laravel\resources\views\testc.blade.php) "
Thank you, but I get another error (after rectification) :
ErrorException in FileViewFinder.php line 137: View [app] not found. (View: C:\wamp64\www\Laravel\resources\views\testc.blade.php) "
Sounds like you've modified the source of testc.blade.php and you're trying to extend or include a view that doesn't exist.
Please or to participate in this conversation.