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 ....
Related
I have a little problem with Laravel Blade in Laravel version 5.2. Can someone tell me how to include a Blade template within a Blade template? I already try to include welcome.blade.php
by #include('welcome') but, I always get an error message.
first you need to define a master page or main template what ever you say.
we create main.blade.php as template page .
<html>
<div class="row">
#yield("content")
</div>
the above code is our simple template page.
inside your welcome.blade.php write as below
#extends("main")
#section("content")
your code
#stop
if welcome.blade.php is your template or master page do like below.
welcome.blade.php :
<html>
<div class="row">
#yield("content")
</div>
inside your other page you want use welcome page as template do as below :
#extends("welcome")
#section("content")
your code
#stop
I have a sidebar include file that is present on every page of the website I am working on.
layout.blade:
<div class="after-login buying-process-wrapper">
<!--page content-->
{{ $content }}
<!-- end of page content-->
<!-- Sidebar -->
#include('layout.sidebar')
<!-- End of Sidebar -->
</div>
My controller sets $layout and renders the above blade file, but layout.sidebar is an include file which requires PHP to populate it's content.
Can I set a controller/route for this sidebar alone (and how would I do it?), or am I forced to have to duplicate the same calls to the function that handles the sidebar content in every controller?
I'm trying to find a better solution than having to go in to every controller and calling ->sidebar() every time each page is loaded.
Thanks
Why are you still using Laravel 4.2? It't 5.7 now and I strongly recommend you to upgrade to the latest version.
Answer to your question:
No, you don't need to call functions that handle your sidebar contents in every controller. You can share you common data across all or some of your views by using View Composer.
From Laravel 4.2 Documentation:
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want bound to a given view each time that view is rendered throughout your application, a view composer can organize that code into a single location. Therefore, view composers may function like "view models" or "presenters".
If you are using newer versions of Laravel, remember to switch to the right documentation from the right upper corner.
Also, when using Blade #include directive, you can optionally pass variables into the "included" components:
#include('layout.sidebar', ['my_var' => 'value goes here'])
And you can use {{ $my_var }} in your component just like you normally would in your blade templates.
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 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.
I'm trying to use sections and templates using Laravel 5.1, however when sending emails the HTML template does not get parsed.
For example the following code results in just the content part being sent and no HTML template (emails.template) with it;
#extends('emails.template')
#section('content')
Hi there!<br><br>
Please click on the link below to reset your password:<br><br>
{{ url('password/reset/'.$token) }}<br><br>
<strong>Note:</strong> This is an automatically generated email, please do not reply.
#endsection
My emails/template.blade.php file looks like this:
#yield('header')
#yield('content')
#yield('footer')
What could I be doing wrong?
As shown, the issue is that your layout is expecting the view that extends it to define the 'header', 'content', and 'footer' sections. Since your view only defined the 'content' section, that is all that is going to be shown.
If you have emails/header.blade.php and emails/footer.blade.php partials you would like to show, this is not the correct syntax. To do that, you would need to #include these partials. You can include them inside sections, if you want the view to be able to overwrite them, though.
You're looking for something like the following:
#section('header')
#include('emails.header')
#show
#yield('content')
#section('footer')
#include('emails.footer')
#show
Now, your 'header' and 'footer' sections will default to what is included in your emails/header.blade.php and emails/footer.blade.php files, but they can still be overwritten (or appended to) in the actual view by defining it's own 'header' and 'footer' sections.