The Laravel Blade Templating Engine

  • Seperation 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:

```

```

Sending Multiple Variables

In the Controller:

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

In the View:

Your User is on

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,

Escaping Dangerous Input

With the standard `` syntax script tags etc are automatically sanitised

To make the output raw use the following syntax:

{!! 'My List <script>alert("spam spam spam!")</script>'}

Looping over an Array

```

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

```

An array could be empty so use a forelse

```

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

```

Conditional Logic

Quite stange syntax

``` @if (count($lists) > 1)

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

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

You have one list:

@else

You don't have any lists saved

@endif

Comments

```

```