Laravel Templating

Well organized views and templates.

 

  • DRY views
  • Make view components reusable
  • Quickly pass data throughout our app

Displaying Data

// normal php data
<?php echo $username ?>
// with blade tags
{{ $username }}

Displaying Data

// ternary statement
{{ isset($title) ? $title : 'My App' }}
// default value
{{ $title or 'My App' }}

Blade Benefits

  • Keep views clean
  • No advanced PHP logic in views
  • Limited control structures
  • Easy includes and templating

Control Structures

if

@if ($users->count() > 0)
    show the users
@else
    No users!
@endif

foreach

@foreach ($users as $user)
    The user is {{ $user->name }}
@endforeach

Blade Templating

  • layouts
  • pages
  • partials

Extending Layouts


@extend('layouts.main')

Extending Layouts


@yield('content')
@section('content')
    the content goes here
@endsection

Including Child Views


@include('partials.header')

Echo Comparison

// normal php data
<?php echo $username ?>
// with blade tags
{{ $username }}

Stacks

@stack('scripts')
@push('scripts')
    <script src="js/dashboard.js"></script>
@endpush

Laravel Templating

By Chris Sevilleja

Laravel Templating

  • 1,208