Skip to content

Laravel Blade Templating Engine

The Laravel Blade Templating Engine#

  • Separation of Concerns
  • Simplified syntax for embedding logic in views

for laravel to recognise a file as blade it needs to end with: *.blade.php

Adding Variables to Views#

In the controller:

public function index(){
  return view('index')->with('name', 'Surfer190');
}

or with a magic method:

public function index(){
  $name = 'Surfer190';
  return view('index')->withName($name);
}

In the View:

<p>{{ $name }}</p>

Sending Multiple Variables#

In the Controller:

$data = array('name => 'Surfer190',
              'gitserver' => 'github.com');
return view('index')->with($data);

In the View:

Your User is {{ $name }} on {{ $gitserver }}

The above can get quite messy so it is suggested to use PHP Compact

Eg.

$name = 'Surfer190';
$gitserver = 'Github';

return view('index', compact('name', 'date'));

## Setting Default Values in View

Hello, {{ $name or ‘my man’ }}


## Escaping Dangerous Input

With the standard `{{ ... }}` syntax script tags etc are automatically sanitised

To make the output `raw` use the following syntax:

{!! ‘My List ‘}


## Looping over an Array

    @foreach ($list as $list)
  • {{ $list }}
  • @endforeach

An array could be empty so use a `forelse`

    @forelse ($list as $list)
  • {{ $list }}
  • @empty
  • You don't have any lists saved
  • @endforelse

## Conditional Logic

Quite stange syntax

@if (count($lists) > 1)

    @foreach ($list as $list)
  • {{ $list }}
  • @endforeach

@elseif (Count($lists) == 1)

You have one list: {{ $list[0] }}

@else

You don't have any lists saved

@endif

Comments#

{{-- This is a Comment --}}