Automatic Blade variables

Automatic Blade variables

Slot/s

Component Slot/s are rendered by echoing the variable: “$slot“. We can pass content into the slot by injecting it into the component.
Like so:

Lets say we have a layout file: “layout.blade.php”

PHP
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home Page</title>
</head>
<body>
    <div>
        {{ $slot }}
    </div>
</body>
</html>

Then you also have a views file: “home.blade.php”

PHP
<x-layout>
    <h1>Hello, World</h1>
</x-layout>

Now what that does it takes this: “<div><h1>Hello, World</h1></div>” and echo’s it where the “{{ $slot }}” is. you can think of the “{{}}” like a php-echo like the “<?= ?>” php-echo shortcut.
Now what these two code examples above do is the same as this:

PHP
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home Page</title>
</head>
<body>
    <div>
        <h1>Hello, World</h1> // {{ $slot }}
    </div>
</body>
</html>

Now here it might not make much sense on why you should do it like this, but once you have a lot of views it will make things a lot easier and more comfortable to adjust and see / understand.

Now you might say that what if i use multiple “$slot” components. How will it know which one im referencing? Well you can modify the slot component for a different slot by naming them. Here’s how:

Lets say we have a layout file: “layout.blade.php”

PHP
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home Page</title>
</head>
<body>
    <div>
        {{ $title }}
    </div>

    {{ $slot }}
</body>
</html>

Then you also have a views file: “home.blade.php”

PHP
<x-layout>
    <x-slot:title>
            <h1>Hello, World</h1>
    </x-slot>
    
    <p>Lorem ipsum dolor sit amet consectetur adipiscing elit quisque faucibus ex sapien vitae.</p>
</x-layout>

You may define the content of the named slot using the x-slot tag.
Any content not within an explicit x-slot tag will be passed to the component in the $slot variable.
Now what these two code examples above do is the same as this:

PHP
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home Page</title>
</head>
<body>
    <div>
        <h1>Hello, World</h1> // {{ $title }}
    </div>
    
    <p>Lorem ipsum dolor sit amet consectetur adipiscing elit quisque faucibus ex sapien vitae.</p> // {{ $slot }}
</body>
</html>

attributes

Now the attributes component is basically the same as the slots component. The variable is $attributes but instead of just injecting whatever this is used for attributes like: href=””, src=””, class=””, id=””, etc.

props

The @props directive lets you list attributes that should be treated as data variables / properties (props). What that does is that they will no longer be passed through the “$attributes” variable / component.

PHP
@props([
    'type' => 'submit',  // default value
    'text',              // required – no default provided
])
  • Each array key becomes a local variable ($type, $text) you can echo anywhere in the template.
  • Default values ('submit') are used when the caller omits the attribute.
  • Leaving out a default makes the prop required; Laravel will throw an exception if the caller doesn’t pass it.
  • Anything not listed here is still available via {{ $attributes }} for transparent pass-through to your root element.

Now when you call a prop name and give it a value of “false” it will be interpreted as a string not a Boolean. So what you can do it prefix it with a colon (:). Because in Laravel and Blade when you prefix a prop name with a colon (:) it indicates that the value that you provide should be treated as an expression rather than a string.

Tags: