livewire / volt

Volt is an elegantly crafted functional API for Livewire.
https://livewire.laravel.com/docs/volt
MIT License
327 stars 19 forks source link

Dynamic page title for class based Volt component #46

Closed onemoreahmad closed 11 months ago

onemoreahmad commented 1 year ago

I don't know if I'm missing something or this is just not implemented.

I need to add a title to a full page Volt component. I have this home page component

<?php

use Livewire\Attributes\{Title, Layout};

new class extends Livewire\Volt\Component {

#[Layout('components.layout')]

// this works, but for static title, 
// also works only if there is a function underneath it! so I had to add an empty with function to get rid of the error
#[Title('Home')]
function with(){ return [];} // it's here just to show the title!

// This doesn't work
// function title(){
    return __('Translated page title');
}

} ?>

<div>
    Home page
</div>

I render this component directly as volt route Volt::route('/', 'home');

jdion84 commented 1 year ago

i believe you can use title method above your class e.g.:

use function Livewire\Volt\{state, title};

title(__('Translated page title');

// new class extends...
onemoreahmad commented 1 year ago

@jdion84 I tried this before, it doesn't work.

egekibar commented 12 months ago

try this;

use function Livewire\Volt\{state, title};

title(__('Translated page title'));