Blade for PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Blade for PHP: Complete Guide with Examples

\n\n

1. What It Is and Why It's Needed

\n

Blade is a powerful and elegant templating engine that is part of the Laravel framework. It provides a convenient syntax for working with HTML templates, allowing developers to separate presentation logic from application business logic. Unlike plain PHP, Blade offers concise constructs for loops, conditions, template inheritance, and component insertion, making the code more readable and maintainable.

\n\n

The main task of Blade is to simplify the creation of dynamic web pages. It compiles templates into plain PHP code, which is cached until the template is modified. This ensures high performance, as recompilation is not required for each request. Blade also includes built-in protection against XSS attacks through output escaping (the {{ }} function).

\n\n

Blade is ideal for Laravel projects, but can also be used standalone (via the jenssegers/blade package). It supports template inheritance (layout), sections, components, slots, conditional directives, and much more. Thanks to its simplicity and flexibility, Blade has become the de facto standard for templating in the PHP ecosystem.

\n\n

2. Installation

\n

Blade comes bundled with Laravel, so no additional installation is required for use within the framework. If you want to use Blade standalone (e.g., in a project without Laravel), install the package via Composer:

\n\n
composer require jenssegers/blade
\n\n

After installation, you need to create an instance of the Blade class and specify the paths to templates and cache:

\n\n
use Jenssegers\\Blade\\Blade;\n\n$blade = new Blade('views', 'cache');\necho $blade->render('index', ['name' => 'World']);
\n\n

Where views is the folder with templates, and cache is the folder for cached files.

\n\n

3. Quick Start — Minimal Working Example

\n

Create a template file views/hello.blade.php:

\n\n
<!DOCTYPE html>\n<html>\n<head>\n    <title>Hello, {{ $name }}!</title>\n</head>\n<body>\n    <h1>Welcome, {{ $name }}!</h1>\n    @if (strlen($name) > 5)\n        <p>You have a long name!</p>\n    @else\n        <p>Short and sweet name.</p>\n    @endif\n</body>\n</html>
\n\n

Now create a PHP script for rendering:

\n\n
require 'vendor/autoload.php';\n\nuse Jenssegers\\Blade\\Blade;\n\n$blade = new Blade('views', 'cache');\necho $blade->render('hello', ['name' => 'Alexander']);
\n\n

Result: An HTML page with a greeting and a conditional message.

\n\n

4. Main Methods/Functions/Classes

\n\n

4.1. @extends — Template Inheritance

\n

Signature: @extends('layout.name')

\n

What it does: Indicates that the current template inherits another (parent) template.

\n

Example:

\n
<!-- layouts/app.blade.php -->\n<html>\n<head>\n    <title>@yield('title')</title>\n</head>\n<body>\n    @yield('content')\n</body>\n</html>\n\n<!-- pages/home.blade.php -->\n@extends('layouts.app')\n\n@section('title', 'Home Page')\n\n@section('content')\n    <h1>Welcome!</h1>\n@endsection
\n\n

4.2. @section and @yield — Sections

\n

Signature: @section('name', 'default') and @yield('name', 'default')

\n

What it does: Defines a content section that will be inserted into the parent template via @yield.

\n

Example:

\n

  
  

Recommendations