layout of view page how to arrange the page
Views
The File Structure
Let's create the files necessary for creating a whole templating system. Here are the folders and files. Go ahead and create them.
- app -- views --- layouts ------- default.blade.php ------- sidebar.blade.php --- pages ------- home.blade.php ------- about.blade.php ------- projects.blade.php ------- contact.blade.php --- includes ------- head.blade.php ------- header.blade.php ------- footer.blade.php ------- sidebar.blade.phpIncludes
We will use the head, header, and footer includes so that we don't have to rewrite that code. Let's get those out of the way real quick.
head.blade.php
<meta charset="utf-8"> <meta name="description" content=""> <meta name="author" content="Scotch"> <title>Super Cool Layouts</title> <!-- load bootstrap from a cdn --> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/3.0.3/css/bootstrap-combined.min.css">header.blade.php
<div class="navbar"> <div class="navbar-inner"> <a id="logo" href="/">Single Malt</a> <ul class="nav"> <li><a href="/">Home</a></li> <li><a href="/about">About</a></li> <li><a href="/projects">Projects</a></li> <li><a href="/contact">Contact</a></li> </ul> </div> </div>footer.blade.php
<div id="copyright text-right">© Copyright 2013 Scotchy Scotch Scotch</div>Default Layout and Pages (Home, Contact)
With our includes ready, let's make our first layout.
We will be using
@includeto bring in slices and@yieldto bring in content from the individual pages we will be using.<!doctype html> <html> <head> @include('includes.head') </head> <body> <div class="container"> <header class="row"> @include('includes.header') </header> <div id="main" class="row"> @yield('content') </div> <footer class="row"> @include('includes.footer') </footer> </div> </body> </html>Home Page and Contact Page
We won't dive too far into the actual content of each page. The home page and contact pages will use the same
layouts/default.blade.php. We won't have to reuse the code in the layout or the includes now!Blade lets us use the layout that we just created by using
@extends. By creating@section, we create a section that will be used in the layout. Here we use@section('content')and in our layout, all that we type here will be injected in@yieldin the layout.pages/home.blade.php
@extends('layouts.default') @section('content') i am the home page @stoppages/contact.blade.php
@extends('layouts.default') @section('content') i am the contact page @stopSidebar Layout and Pages
With our default layout and those pages out of the way, we can make our sidebar include, sidebar layout, and pages.
Sidebar Include includes/sidebar.blade.php
<!-- sidebar nav --> <nav id="sidebar-nav"> <ul class="nav nav-pills nav-stacked"> <li><a href="#">Fly to the Moon</a></li> <li><a href="#">Dig to China</a></li> <li><a href="#">Swim Across the Sea</a></li> </ul> </nav>Sidebar Layout layouts/sidebar.blade.php
The difference between this layout and the default layout is the sidebar. We add that to the main section with the help of Twitter Bootstrap classes. Now we have a sidebar on the left and our content on the right. We can adjust this however we want to create any size sidebar either on the left or the right.
<!doctype html> <html> <head> @include('includes.head') </head> <body> <div class="container"> <header class="row"> @include('includes.header') </header> <div id="main" class="row"> <!-- sidebar content --> <div id="sidebar" class="col-md-4"> @include('includes.sidebar') </div> <!-- main content --> <div id="content" class="col-md-8"> @yield('content') </div> </div> <footer class="row"> @include('includes.footer') </footer> </div> </body> </html>About and Projects Pages
Like the home and contact pages, we won't dive into the content. Just a simple use of the layout and adding content.
pages/about.blade.php
@extends('layouts.sidebar') @section('content') i am the about page @stoppages/projects.blade.php
@extends('layouts.sidebar') @section('content') i am the projects page @stopConclusion
Now we can create a simple foundation for the front-end views for our site. Layouts, slices, and pages all work together to create an easy templating system. There is much more that and I encourage you to take a look at what else we can do.
Check out our other Laravel tutorials in our for more tutorials on our favorite PHP framework.
0 Comments