I have the site with the structure like:
First: master.blade.php : this contain section('content')
<body>
#include('partial.header')
#yield('content')
#include('partial.footer')
</body>
Second index.blade.php : contain section('content').
#extends('layouts.master')
#section('content')
<div id="container">
<div id="news">
#yield('news')
</div>
<div id="apartment">
#yield('apartment')
</div>
</div> <!-- ./container -->
#endsection
Third: news.blade.php : this simple to show all news
#foreach($posts as $post)
#endforeach
Final file: apartment.blade.php : this simple to show all apartment.
#foreach($apartments as $apartment)
#endforeach
My route direct to master.blade.php.
My question is:
When I include news with #yield('news') in index.blade.php. It shows correct all news in my database.
But when I delete #yield('news') in index.blade.php. It also show news from my database (but it's lost css/js for that).
Why I deleted #yield('news'), it's should don't show any news on my page?
Seem Laravel Blade not support two #yield in #section. When I add only 1 row #yield('news') into index.blade.php file. It shows list news on my index page. When I continues add #yield('apartment'). Don't have any apartment shown on the index page. I certainly it has values when foreach to get data. I also test with HTML statics but don't have anything changes.
My route direct to master.blade.php.
Index extends master, so point your route to index view.
If sections continue to be yielded, after #yield removal, I think the framework is loading a cached view.
Try clearing the view cache php artisan view:clear or use php artisan --help view:clear for help.
Also #yield yields a section in a parent child relationship.
Change yields to include like #include('partial.news') if news are in partial folder or to whatever the path. Rendering could be manipulated using #isset or #empty statements or even #forelse loops.
Blade Control Structures.
Related
I recently started to build a Laravel/blade web application and I want to switch between what my layout view is showing. I want to replace 'content' with some other blade.php view when I press a button in the layout file. For example in ReactJS you can just determine the rendered content with an IF statement and some vars.
<div class="container">
#yield ('content')
</div>
I googled a bit but couldn't find a straight forward solution so I wondered if this is common in Laravel or do you just have to make a lot of different layout files with other #yield('...')? A lot of code would be duplicated right?
You can use conditional blade directives
#if(Session::get(user_type') == 'Admin')
#extends('layouts.admin')
#else
#extends('layouts.normal')
#endif
#section('title')
#endsection
#section('content')
etc ....
I want to import partial template in other partial.
For example, I'm already using header partial.
Current Structure is
-- header.blade.php
--+ partials
|__ nave.blade.php
In this case, I want to use nav partial in header partial.
But I was not available to add nav to header. I could see not found error.
Is there any special way to solve this issue?
Assuming you have a main.blade.php page.
The content is like this:
#include('layouts.header')
<section> Main Content {{$slot}} </section>
#include('layouts.footer')
In header.blade.php, here is your content:
<div class="container">
<div id="app">
#include('layouts.nav')
On your nav.blade.php page, here could be your content:
<nav id="navbar"> Nav Item </nav>
Then finally, in your footer partial, you can close the opened divs up:
</div>{{-- Closes the #app id --}}
</div> {{-- Closes the Container class --}}
<script>Your JS links and sources</script>
So, in essence, what all these suggest is that within any file, if you call the file path correctly, you can use and
embed any partial from anywhere. Note, the (dot) indentation suggests a level deep down in the files structure.
Which means, in your Views folder, this will be the structure of your subfolders:
- Views
- layouts.blade.php
- header.blade.php
- nav.blade.php
- footer.blade.php
- home.blade.php
I was hoping someone could help me with my understanding when using the #sectoin and #yield commands inside a themosis 1.2 scout template.
Basically I have a view "/views/my-page.scout.php" with some basic html markup:
#include('includes.header')
<div> some content </div>
#yield('extra-content')
#include('includes.footer')
enter code here
Then inside of another file located in "views/extras/extra-content.scout.php" I have the following:
#section('extra-content')
<div>Some extra content</div>
#stop
Im not sure why my #yield is not working, I know I could just use #include but I wanted to get a better understanding of using #yield. Ive checked out the laravel and themosis documentation but im still confused.
Any help would be most appreciated. :)
File location: /views/extras/extra-content.scout.php
File name: extra-content.scout.php
File Contents:
#section('extra-content')
<div>Some extra content</div>
#stop
File location: /views/my-page.scout.php
File name: my-page.scout.php
File Contents:
#include('includes.header')
<div> some content </div>
#yield('extra-content')
#include('includes.footer')
File : my-page.scout.php
#include('includes.header')
#include('extras.extra-content')
<div> some content </div>
#yield('extra-content')
{{-- or include here #include('extras.extra-content') --}}
{{-- or include here #include('extras.extra-content') --}}
#include('includes.footer')
the file extra-content should be included explicitily in this file or any file it's extending/including.
yield will just store the content in a variable with will be available in this file.
In your use case, including may be the best way, because your file contains only that section. imagine a extra-content file where you will output some content and handle 'extra-content` variable
section content will always be placed where you yield it.
this code
#section('my-content')
i want to place to place this content somewhere
#stop
will be interpreted as:
$sections['my-content'] = 'i want to place to place this content somewhere';
and
yield('my-content');
is intepreted as
echo isset($sections['my-content']) ? $section['my-content']:'';
Edit imagine your file extra-content where you define the section doesn't contains only the section definition:
File: extra-content
#section('my-content')
this is yielded content displayed where you use yield('my-content')
#stop
<p> this will be displayed where the file is included</p>
I want to show the result when search a keyword successful.
In routes\web.php:
Route::get('tim-kiem', 'Frontend\ListBaiVietController#timkiemBaiViet');
In controller ListBaiVietController, I have a function:
public function timkiemBaiViet() {
$tukhoa = \Request::get('tukhoa');
$ketquatimkiems = Post::where('title','like','%'.$tukhoa.'%')
->orderBy('title')
->paginate(20);
// var_dump($ketquatimkiems);
return view('post/searchresult',compact('ketquatimkiems'));
}
I am using var_dump($ketquatimkiems), it shows 2 results.
In post/index.php I am calling content:
<body>
#yield('content')
</body>
And post/searchresult.php:
#extends('post.index')
#section('content')
#foreach($ketquatimkiems as $ketqua)
<div class="container-artical">
<div class="list-excerpt">
{!! $ketqua->excerpt !!}
</div>
</div>
#endforeach
<nav class="blog-pag">
{{ $ketquatimkiems->links() }}
</nav>
</div>
#endsection
When I am typing text quận 8. It is only showing code, not result.
your file name must have .blade extension
post/searchresult.php: to post/searchresult.blade.php:
post/index.php to post/index.blade.php
for more information
Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file
Ref: https://laravel.com/docs/5.5/blade
you are not using laravel blade engine. Rename view
files like so index.blade.php
I tried to use the laravel's template system: blade but seems like not working when using the code below in the file users.blade.php:
#extends('layout')
#section('content')
Users! #stop
and browser,
#extends('layout')
That should work if you have a template file at /app/views/layout.blade.php that contains
<p>Some content here</p>
#yield('content')
<p>Some additional content here</p>
Then in your /app/views/user.blade.php, the content
#extends('layout')
#section('content')
<p>This is the user content</p>
#stop
If you call return View::make('user') you should have the compiled content
<p>Some content here</p>
<p>This is the user content</p>
<p>Some additional content here</p>
I hope that helps clarify things for you. If not, can you provide your template file locations and the relevant content?
Just remove the extra space or anything before #extends('yourlayoutfile').
It should be the first thing to be rendered in the file.
I was facing the same problem and tried many things.Suddenly I found a single space at the starting of the file before #extends.
Removed the space and is working fine.
Thanks.
Format:
#extends('layouts.default')
#section('content')
.....
#stop
---Edit----
If this didnt work then try :
Copy all the content in the file and then delete the file.
Create a new file and save it as filename.blade.php
Only after saving the file paste the content into the page.
Save the changes and run it.
This works.
Thank you.
Where is your layout?
If its in app/views/layouts, then it should be
#extends('layouts.index')
(assuming the name is index.blade.php)
ex: #extends('layouts.foo') equals a file in app/views/layouts/ called either foo.blade.php or foo.php. (depending if you use blade)
I have the same problem. What is did is:
1. in routes.php
Route::get('about', 'AboutController#index');
that
AboutController is a controller file AboutController.php in app/controllers
index is a function inside that controller.
2.Create AboutController.php in app/controllers
class class AboutController extends BaseController {
protected $layout = 'layouts.default';
$this->layout->content = View::make('pages.about');
}
You can look at this reference: Defining A Layout On A Controller
By default,Laravel has a layouts folder inside views folder, i.e. app/views/layouts and in this folder you keep your layout files, i.e. app/views/layouts/index.master.php and if you have something similar then you should use something like this:
#extends('layouts.master')
#section('content')
<p>Page Content</p>
#stop
This will inherit/use the master.blade.php file (as layout) from layouts folder, here, layouts.master means layouts/master.blade.php.
In your master.blade.php file you mast have this
#yield('content')
So, data/content from the view between #section('content') and #stop will be dumped in the place of #yield('content') of your layout.
You can use any name for your layout file, if it's layouts/main.blade.php then you should use
#extends('layouts.main')
Make sure you inserted the css link in App.blade.php
For me By default there is no link to the css file
Insert the following link in app.blade.php
<link rel="stylesheet" href= "/css/app.css" >
now its works fine :)
list things to make sure
file name and path properly given
double-check .blade.php file extention
layouts.admin.blade.php
<section class="content" style="padding-top: 20px">
#yield('content')
</section>
#extends('layouts.admin')
#section('content')
<p> this is Order index view</p>
#endsection
Try this!
php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan clear-compiled
let's say you have 'master.blade.php' and 'index.blade.php'.
and both of files are in views->home directory. when you want to use #extends in 'index.blade.php' by calling 'master.blad.php' , you should write in index.blade.php file this statment:
#extends('home.master')
not
#extends('master')
Simply save your source using encoding UTF-8 without signature.