kminek / laravel-title
Probably the easiest way to add dynamic HTML title to Laravel app
Requires
- php: >=8.2.0
- illuminate/container: 11.*
- illuminate/support: 11.*
Requires (Dev)
- laravel/pint: 1.20.0
This package is auto-updated.
Last update: 2025-01-19 11:55:25 UTC
README
Probably the easiest way to add dynamic HTML title to Laravel app
Installation
Run the following command from your terminal:
composer require kminek/laravel-title
or add this to require
section in your composer.json
file:
"kminek/laravel-title": "^1.0"
then run composer update
Version compatibility
Usage
Modify your layout view (for example resources/views/layouts/app.blade.php
):
<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>{{ title() }}</title> </head> <body> <main> @yield('content') </main> </body> </html>
Modify your controller action view (for example resources/views/auth/register.blade.php
):
@extends('layouts.app') @title('Register') @section('content') <h1> Register </h1> ... @endsection
Advanced usage
Change default separator (|
):
<title>{{ title()->separator(' - ') }}</title>
Change default text (config('app.name')
):
<title>{{ title()->default('AppName') }}</title>
Set a description which is displayed when no title is set inside controller action view:
<title>{{ title()->default('AppName')->description('This app will blow your mind!') }}</title>
You are not required to use @title()
Blade directive, you can call
library directly which opens more possibilities:
@extends('layouts.app') @php(title()->append('Register')) {{-- append (same behaviour as Blade directive) --}} @php(title()->prepend('Register')) {{-- prepend --}} @php(title()->set('Register')) {{-- replace whole stack --}} @section('content') <h1> Register </h1> ... @endsection
You can have multiple title "stacks" under different keys. Default stack is using "default" as a key, but you are free to create new ones:
<title>{{ title('homepage') }}</title>
and later in Blade view:
@php(title('homepage')->append('Register'))