How can I make content area change on different requests? - php

I have a template like this.
<div class="content">
#yield('content') //this area should load different files on different URI's
</div>
If I load .com/register, it should load register.blade.php at the place of #yield. If I load something else, it will load that view.
I'll define which file should be loaded in Routes.php with Route::get();
Full source is here for easier readability: http://pastebin.com/t2Md20r9 so you can see what I did so far.
What should be done?

You're very close, just extend your layout in register.blade.php.
1.Put your template file in views/layouts/master.blade.php
2.In your register.blade.php put
#layout('layouts.master')
in Laravel 4
#extend('layouts.master')
on top.
3.Now use return View::make('register');

You can pass it in your Route.php file something like this:
Route::get('your_page', function() {
View::make('your_page')->with('contentTemplate', 'register');
}
Route::get('other_page', function() {
View::make('other_page')->with('contentTemplate', 'other_content');
}
And in your_page, do
<div class="content">
#render($contentTemplate)
</div>

Related

Fetch data from inside Blade view

I'm learning about Laravel. I know how to create a controller that builds an array of data from an Eloquent model, pass that array to a view and display it.
What I am trying to do is create a main view that has the header and footer information all pages use. Then I try to use a child view to display the body.
The main blade file has an include for a file that builds a dynamic navigation bar.
How can I go about retrieving data from within the include file the main blade file that's called by a child?
What I have (very abbreviated) is:
main.blade.php
<html>
#include('header')
<body>
#section('content')
#stop
</body>
</html>
home.blade.php
#extends('main')
#section('content')
<h1>Home</h1>
#endsection
Now from the controller, I call the home.blade.php view
Can I fetch the data thats needed in the header view from INSIDE the header file rather than having to pass the data from every view I call? Or do I need to simply create a global function that I call in every controller before I call a view?
In your main page:
<html>
#include('header')
<body>
#yield('content')
</body>
</html>
In your view:
#extends('main')
#section('content')
<h1>Home</h1>
#endsection
The dynamic navigation bar you mention should come from a view composer. In this way it will be available all the time to all views.
Do a test with the following in your routes.php file:
View::composer('*', function($view)
{
// $navBar stuff
$view->with('navBar', $navBar);
});
Now $navBar will be available in your views.
http://laravel.com/docs/5.1/views#view-composers

Laravel master page not working

I am working on my master page which is currently not working. In browser it returns:
#layout('master') and not key as specified.
code in routes.php
Route::get('/', function()
{
return View::make('index');
});
code in view: index.blade.php
#layout('master')
#section('container')
<h1> Hey </h1>
#endsection
code in view: master.blade.php
<div class="container">
#yield('container')
You need to use #extends('master') in L4 and have it right at the top of your file.
I mean RIGHT at the top, line 1, with no white space around it. This use to be a problem in L3, not sure if it also is the case for L4.
replace #layout with #extends
refer http://laravel.com/docs/templates#blade-templating for Using A Blade Layout

#extends('layout') laravel. Blade Template System seems like Not working

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.

Laravel 4 include multiple views in route

Is it possible to include multiple view in route? What is the best practice for doing this, let say I want config file, header, content, and footer file to join and load in a view? If do it in the route, then I can easily change the content based on route request.
Thank you.
You need to take a futher look at Laravel's Blade templating. With Blade templating, you can create layouts and cascade them onto each other really nicely. For example, let's take the following routes...
app/routes.php
Route::get('about', function()
{
return View::make('about');
});
Route::get('contact', function()
{
return View::make('content')
});
As you can see, we have two different views for those two different request. However, with Blade templating, and sections, we can create a master layout and only change the content that we need. So, here is what our master layout would look like.
app/views/layouts/master.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Site Title</title>
</head>
<body>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
#yield('content')
</body>
</html>
This is our master layout. We have our nav that will always stay the same, our HTML and head and everything that we don't want to write over and over again. But, we are also using yield in blade to accept content and place it there. This is where our actual views come into play from routes.php.
app/views/about.blade.php
#extends('layouts.master')
#section('content')
<p>This is the about me content.</p>
#endsection
We can simply extend the master layout, and place our content within the content section, which we can name anything we want. Same with the other page, contact.
app/views/contact.blade.php
#extends('layouts.master')
#section('content')
<p>This is the contact page content.</p>
#endsection
As you can see, it's not so much as including multiple views...but rather it's about extending different views and putting them together using Blade.
Alternate way to include header / footer in another view will as below.
https://laravel.com/docs/5.3/blade#including-sub-views
<div>
#include('templates.header') // views/templates/header.blade.php
<form>
<!-- Form Contents -->
</form>

Laravel 4 - including a "partial" view within a view (without using Blade template)

In Laravel 3, I used to do this.
<?php render('partials.header'); ?>
This was done in "PHP" views, without using Laravel's Blade templates.
What's the equivalent of this in version 4?
I tried
<?php #include('partials.header'); ?>
This doesn't work.
If I do
#include('partials.header')
I have to save my file as ".blade.php"
How do I include a "subview" without using the blade template?
There are different ways to include a view within a view in Laravel 4. Your choice will depend on any one of the outcomes outlined below...
For Flexibility
You can compile (render) the partial views in the appropriate Controller, and pass these views to the Main View using the $data[''] array.
This may become tedious as the number of views increase, but hey, at least there's a lot of flexibility :)
See the code below for an example:
Controller
...
public function showMyView()
{
/* Header partial view */
$data['header'] = View::make('partials.header');
/* Flexible enough for any kind of partial views you require (e.g. a Header Menu partial view) */
$data['header_menu'] = View::make('partials.header_menu');
/* Footer partial view */
$data['footer'] = View::make('partials.footer');
return View::make('myView', $data);
}
...
View
You can include the partials above as follows (at any position in your View code):
<html>
<head></head>
<body>
<!-- include partial views -->
<?php echo ($header) ?>
<?php echo ($header_menu) ?>
<div id="main-content-area"></div>
<?php echo ($footer) ?>
</body>
</html>
Your partial views will now be added to your main View.
For Simplicity
There's actually a much easier way than using the method above: Simply include this in the html of the view...
View
<html>
<head></head>
<body>
<!-- include partial view: header -->
<?php echo View::make('partials.header') ?>
<div id="main-content-area">
</div>
<!-- include partial view: footer -->
<?php echo View::make('partials.footer') ?>
</body>
</html>
Make sure that the folder structure for the partials is [views/partials/header.php] in order to provide the correct file-path to the View::make() function of Laravel.
WARNING
If you try to pass the $data['page_title'] in a controller, the nested views wont receive the data.
To pass data to these nested views you need to do it like this:
<html>
<head></head>
<body>
<?php
/* Pass page title to header partial view */
$data ['page_title'] = "My awesome website";
echo View::make('partials.header', $data);
?>
<div id="main-content-area"></div>
<?php echo View::make('partials.footer') ?>
</body>
</html>
NOTE
The question clearly stated: "Without using Blade template", so I have made sure to give a solution that does not include any Blade templating code.
Good luck :)
You can nest your partials in views try this
View::make('folder.viewFile')->nest('anyname', 'folder.FileName');
Then access the nested view file from your template {{ $anyname }} this way you don't have to include files in your view and this should work for .php file also.
I am not sure how many people have been using Laravel 4 in this post, since this post, but if you are looking to include partials or separate your view types you can do it with #includes
for example, if you want a partials folder for your header, footer, sidebar etc
create a directory for the partials under
app/views/partials
Then create a partial
app/views/partials/navigation.blade.php
Then in your master template file add the line
#include('partials.navigation')
That is all it takes.
** Bonus you can also pass data to a partial or include nested partials within a partial
I know this is a bit of a late answer, but I figured since I didn't see this solution amongst the other answers it was ok.
If you want to include your header and footer on every page I would add them into the before and after filters. Just go to filters.php in your app folder
App::before(function($request)
{
echo View::make('partials.header');
});
App::after(function($request, $response)
{
echo View::make('partials.footer');
});
When doing it this way you don't need to add anything in the view files to include them.
You can use View's nest function
View::make('default.layout')->nest('header', 'default.header');
Use the third parameter to pass data to the template
View::make('default.layout')->nest('header', 'default.header', ['name' => 'John Doe', 'test' => 'It works!']);
on your views/default/header.blade.php
<div>hey {{ $name }}! {{ $test }}</div>
I am still pretty new to Laravel, but I think the below is pretty ideal ...
Route::get('/', function()
{
$data['title'] = 'sample';
return View::make('page', $data);
});
# /views/partials/header.php
# /views/partials/footer.php
View::composer('page', function($view)
{
$view->with('header', View::make('partials.header', $view->getData()));
$view->with('footer', View::make('partials.footer', $view->getData()));
});
See Laravel View Composers .. http://laravel.com/docs/responses#view-composers
/views/page.php
<?php echo $header; ?>
<div>CONTENT</div>
<?php echo $footer; ?>
From within a view, just echo the other view:
echo View::make('header'); //This will look for a view called header.php

Categories