1

This must be really simple, but since I’m new to Laravel, I’m having difficulties on finding this feature on documentation or in stackoverflow.

I’m going to try to build a very simple example to try to show what I want.

Let’s say I have a file called data.blade.php

@php
$myVariable = 'xyz';
@endphp

Then, I have my layout template file, called: home.blade.php

@extends('data.blade')
@section('content')
    @php
        echo $myVariable;
    @endphp
@endsection

When I try to do this, it returns me an error:

Undefined variable $myVariable (etc)

How can I use a variable defined in another blade file? I used extends to exemplify, but I´m not sure if that is the right architecture.

Can anyone point me to the right way of accomplishing this?

Thanks!

Jorge Mauricio
  • 411
  • 6
  • 18
  • 1
    This might be what you are looking for https://stackoverflow.com/questions/29715813/laravel-5-global-blade-view-variable-available-in-all-templates – Gpak Jun 04 '22 at 21:17
  • Trying out one of the solutions. Seems that the first one is working fine for what I´m looking for. – Jorge Mauricio Jun 04 '22 at 22:12

1 Answers1

0

you are extending data.blade.php and this file need to have some element to serve as template and be extended.

You can take any html5 file and rename this as template.blade.php, but where you want to render a section in your template must have a yield('section-name').

Make a new app.blade.php should look like this

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show
 
        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

Then, in your home.blade.php

@extends('layouts.app')
 
@section('title', 'Page Title')
 
@section('sidebar')
    @parent
 
    <p>This is appended to the master sidebar.</p>
@endsection
 

@section('content')
  @include(data)
   {{$myVariable}}


    <p>This is my body content.</p>
@endsection

From Laravel Doc

Manuel Glez
  • 890
  • 3
  • 12
  • Again - I´m new to laravel, so be patient with my questions. ```@include``` can only be used inside ```@section```? Because, in my case, I would need to move ```@include``` to before the ```@extends('layout')``` in order to accomplish what I´m searching for. And also have the ability to use the variable defined inside ```@include``` anywhere in the template file. – Jorge Mauricio Jun 04 '22 at 22:02
  • You can use include anywhere below extend, but will be displayed in yield content conveniently. – Manuel Glez Jun 04 '22 at 22:07
  • And how about before ```@extends```? Any other mechanism I could use to access that data variable? – Jorge Mauricio Jun 04 '22 at 22:11
  • If you want to pass some data to a view you will, almost always do via an array from the view. – Manuel Glez Jun 04 '22 at 22:12
  • 1
    myControllerMethod() { return view('myview',['myVar'=>'Test']) } – Manuel Glez Jun 04 '22 at 22:15
  • It´s an interesting approach, but not exactly what I was looking for. – Jorge Mauricio Jun 04 '22 at 22:43
  • What are you looking for? What is your prob? – Manuel Glez Jun 04 '22 at 22:49
  • Well, I need a way that I could print a variable that should be defined in ````data.blade.php file```, but that would come before any info in my file - even before ````@extends('layouts.app')```. – Jorge Mauricio Jun 04 '22 at 22:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245330/discussion-between-manuel-glez-and-jorge-mauricio). – Manuel Glez Jun 04 '22 at 23:03