Laravel @yield in code php
Asked Answered
S

1

7

How I can get content from @yield in PHP?

Example I have in app.blade.php:

@yield('image-url', asset('/img/metaog.png?2'))

I want getimagesize from image-url:

<?php 
    $image = getimagesize(yield('image-url', asset('/img/metaog.png?2')));
    $width = $image[0];
    $height = $image[1];
?>

How I can get this correctly? My code is not working.

Sign answered 16/12, 2017 at 16:15 Comment(2)
Seems like you don't understand what is @yield for.Deflagrate
Why would you want to get the content from the @yield directive?Clave
E
15

In Laravel 5.5 and up:

View::getSection('image-url', 'your default value')

In Laravel 5.4 and below:

View::getSections()['image-url']

That will get what was assigned to that named section. You will have to do your check to see if it has anything still. If using the first method you should be checking if that array key actually exists.

You can use View::hasSection(...) to check if the section exists at all, if needed.

Embalm answered 16/12, 2017 at 16:29 Comment(3)
This is not working. I have laravel 5.5. I get null. URL section he see, but image-url - not.Sign
if you get a null, that section wasn't set, or its empty ... updated to include default argument to getSectionEmbalm
In Laravel 10.x (year 2023) it still worksVday

© 2022 - 2024 — McMap. All rights reserved.