0

I'm trying to add a funtionality to a web app where I need the current page header as a variable. Could you please tell me how to process the page below in server before serving and assign the the text inside HTML tag h2 to a php variable '($currentPageHeader)'? Thank you

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->


        <!-- Styles -->
        <style>

        </style>
    </head>
    <body>
        <div class="flex-center position-ref full-height">


            <div class="content">
                <div class="title m-b-md">
                    <h2>Text to be extracted</h2>
                </div>

                <div class="links">

                </div>
            </div>
        </div>
<?php $currentPageHeader = "Text to be extracted"; ?> // Content of <h2>
    </body>
</html>
root
  • 157
  • 1
  • 3
  • 16

1 Answers1

1

create master.blade.php

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->


        <!-- Styles -->
        <style>

        </style>
    </head>
    <body>
        <div class="flex-center position-ref full-height">


            <div class="content">
                <div class="title m-b-md">
                    @yield('content')
                </div>

                <div class="links">

                </div>
            </div>
        </div>
    </body>
</html>

then you can extend that

@extends('master')

@section('content')
    <p>This is my body content.</p>
@stop

ref link https://laravel.com/docs/5.0/templates#blade-templating

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33
  • I think u misunderstood what I meant. I've edited the question. Could u please have a look again? Thanks so much – root Jan 03 '20 at 14:47
  • you should create header.blade.php and extend that – Kamlesh Paul Jan 03 '20 at 14:55
  • I want to extract the header of every page as a PHP variable. The blade files are already finished. I just need to add php code to that to get the header as a variable. – root Jan 03 '20 at 14:58
  • gor it now laravel view composer is your solution check this out https://laravel.io/forum/11-02-2016-view-composer-to-pass-variable-to-all-views – Kamlesh Paul Jan 03 '20 at 14:59
  • Thank you. Will look into that. But isn't there a simple php code I can use to get this done? Something like [this?](https://stackoverflow.com/questions/8144061/using-php-to-get-dom-element). I can't figure out how to load the current page here `$dom->loadHTML(file_get_contents('http://localhost/index.php'))` – root Jan 03 '20 at 15:38