---
title: Learning Laravel Views
date: 2014-02-26 00:00:00
featured_image: https://images.unsplash.com/photo-1492500318351-274a118407c2?q=90&fm=jpg&w=1000&fit=max
excerpt: Laravel like any php framework separate visual aspects of your application from business logic .Before we get started to explore templating syntax . I will show you how to render view files from a route or controller.
---

Laravel like any php framework separate visual aspects of your application from business logic .Before we get started to explore templating syntax . I will show you how to render view files from a route or controller.
```php
/* render app/views/hello.php */
Route::get('/', function()
{
return View::make('hello');
});
```
As you can see, I used `View::make('hello')` which will render `app/views/hello.blade.php`. Now let's explore templating syntax.
### Output Data
You probably use `echo` function to output data in PHP .In laravel ,just surround variables by curly brackets.
```php
{{ date('d/m/y') }}
```
### Control Structures
Laravel has alternative syntax for control structures .Now i will pass array of data to `app/views/posts.blade.php`.
```php
/* app/routes.php */
Route::get('posts', function()
{
$posts=array(
'total'=>'2',
'data'=>array(
'laravel routes'=>array(
'date'=>'2/24/2014',
'author'=>'Mark',
'category'=>'php'
),
'laravel views'=>array(
'date'=>'2/22/2014',
'author'=>'John',
'category'=>'php'
)
//.....
)
//...
);
return View::make('posts',array('posts' => $posts,'var' => 0));
});
```
These data will be available `app/views/posts.blade.php` .Let's explore laravel control structure syntax.
```html
Posts
{{-- output data --}}
{{ $posts['total'] }}
{{-- control structures --}}
@if ($posts['total'] > 3)
@endwhile
```
### Simple Templating
You could break views into separate files for organisational purposes and minimizing repetition .For example ,if page header don't change in your views files ,you could create file for header `app/views/header.blade.php`.
```html
Posts
```
Now you can include this header in other view files for example `app/views/posts.blade.php` like that.
```html
@include('header')
```
### Advanced Templating
Let's imagine you create a portfolio with two layouts (left sidebar layout and right sidebar layout) and overall page structure of two layouts is similar except two sections (header and projects div) .Let's define these words into code.
```html
Portfolio
@section ('header')
@show
@yield('projects')
```
```html
Portfolio
@section ('header')
@show
@yield('projects')
```
We created two layouts and defined the two sections which change .we used `@yield()` method to create a section that we can fill with content later .The other method `@section()` is similar to `@yield()` except that the content provided between `@section` and `@show` tags used as default value for header section.
Now let's create two pages .One for each layout.
```html
@extends('layouts.leftsidebar')
@section('header')
@stop
@section('projects')
@stop
```
The complete source code of two pages will look like this.
```html
Portfolio
Left sidebar projects
```
```html
Portfolio
Right sidebar projects
```
As you can see ,when you provide content for header section ,this content will override default value .When you don't provide value to header section ,default value will be used .What if i need to provide value for header section and insert default value ..! just use `@parent` like that.
```html
@extends('layouts.leftsidebar')
@section('header')
@parent
@stop
@section('projects')
Left sidebar projects
@stop
```
So source code will look like this.
```html
Portfolio
Left sidebar projects
```
### Comments
You can insert notes in your views using blade comments syntax like that.
```html
{{-- you comment here --}}
```