Loading a view into another view/nested views in laravel

Very often, our web pages will have a similar layout and HTML structure. To help separate out
the repeated HTML, we can use nested views in Laravel.

Getting ready

For this recipe, we need to have completed the Creating and using a basic view recipe.

How to do it…

To complete this recipe, follow these steps:
1. In the app/view directory, add a new folder named common.

2. In the common directory, create a file named header.php and add the following
code to it:
<!doctype html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>

<title>My Website</title>
</head>
<body>
3. In the common directory, create a file named footer.php and add the following
code to it:
<footer>&copy; 2013 MyCompany</footer>
</body>
</html>
4. In the common directory, create a file named userinfo.php and add the following
code to it:
<p>You are <?= $my_name ?>, from <?= $my_city ?></p>
5. In the routes.php file, update the home and second route to include the following
nested views:
Route::get(‘home’, function()
{
return View::make(‘myviews.home’)
->nest(‘header’, ‘common.header’)
->nest(‘footer’, ‘common.footer’);
});
Route::get(‘second’, function()
{
$view = View::make(‘myviews.second’);
$view->nest(‘header’, ‘common.header’)->nest(‘footer’,
‘common.footer’);
$view->nest(‘userinfo’, ‘common.userinfo’,
array(‘my_name’ => ‘John Doe’, ‘my_city’ => ‘Austin’));
return $view;
});

6. In the views/myviews directory, open the home.php file and add the following
code to it:
<?= $header ?>
<h1>Welcome to the Home page!</h1>
<p>
<a href=”second”>Go to Second Page</a>
</p>
<?= $footer ?>

7. In the views/myviews directory, open the second.php file and add the following
code to it:
<?= $header ?>
<h1>Welcome to the Second Page</h1>
<?= $userinfo ?>
<p>
<a href=”home”>Go to Home Page</a>
</p>
<?= $footer ?>
8. Test the views by going to http://{your-server}/home (where your-server is
our URL) and then clicking on the link.

How its Works…

To begin, we need to separate out our header and footer code from our views. Since these will
be the same on every page, we create a subdirectory in our views folder to hold our common
files. The first file is our header, which will hold everything up until the <body> tag. Our second
file is our footer, which will hold the HTML at the bottom of our page.
Our third file is a userinfo view. Very often, if we have user accounts with profiles, we may
want to include the user’s data in a sidebar or header. So that we can keep that one piece of
our view separate, we create the userinfo view with some data that we will pass to it.
For our home route, we will use our home view and nest in the header and footer. The first
parameter in the nest() method is the name we will use in the main view and the second
parameter is the location of the view. For this example, our views are on the common
subdirectory, so we use the dot notation to reference them.
In our home view, to display the nested views, we print out the variable name we used in
our route.
For our second route, we nest in the header and footer as well but we also want to add in the
userinfo view. For this, we pass in a third parameter to the nest() method, which is an
array of the data we want to send to the view. Then, in our main view, when we print out the
userinfo view, it will automatically have the variables included.

Leave a Reply

Your email address will not be published. Required fields are marked *