Bruno Mangilli
Bruno Mangilli

Reputation: 1

How to pass data variables to layouts function in Volt/Blade

I have a problem with Livewire/Volt and Blade in Laravel, I'm using a layout called 'app.blade.php', this is the base layout of application and have the $htmlClass and $bodyClass.
app.blade.php:

<!DOCTYPE html>
@php
    $bodyClass??= [];
    $bodyClass = [
        ...$bodyClass,
        'another utility class'
    ];

    $htmlClass = is_array($htmlClass ??= '') ? implode(' ', $htmlClass) : $htmlClass;
    $bodyClass = implode(" ", $bodyClass);

@endphp
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}" class="{{ $htmlClass }}"> <--- this
    <head>
        ...
    </head>
    <body class="{{ $bodyClass }}"> <--- this

in the register.blade.php:

<?php

$dataToLayout = [
    'htmlClass' => ['h-full'],
    'bodyClass' => ['dark:bg-slate-900', 'bg-orange-600', 'flex', 'items-center'],
];
layout('layouts.app');

I want to pass $dataToLayout to app.blade.php but nothing of I tried works. Any suggestion?

I tried to pass in layout function but this not work it. layout('layouts.app', [...])

Upvotes: -1

Views: 372

Answers (1)

Ayoub ASSAOUD
Ayoub ASSAOUD

Reputation: 150

laravel blades allow only one-direction-inheritance, you cannot import php variables from the child component. but you can try coding in this way:

app.blade.php:

<html lang="{{ str_replace('_', '-', app()->getLocale()) }}" class=" @yield('htmlClass')"> <--- this
<head>
    ...
</head>
<body class="@yield('bodyClass')">

register.blade.php:

@extends('layouts.app');
@section('htmlClass', 'h-full')
@section('bodyClass', 'dark:bg-slate-900 bg-orange-600 flex items-center')

Upvotes: 0

Related Questions