0
votes

It's not really a big problem, but it is mighty annoying.

When starting creating templates in laravel using blade, everything looks fine. The "master" template/layout gets it's "styling" correct. And by "styling" i mean like correct amount of whitespaces, newlines etc when viewing the source code.

The problem occurs when you know try to extend this master template. for every @section('<something>') you do know, all newlines is removed from the code, making the source code look fuggly.

Have been searching this phenomenon for a while without finding anything interesting which explains why or maybe a solution to make the source code readable again.

Here is an example if the explanation wasn't good enough:

// master.blade.php
<html>
<head>
    <title>Something here</title>
</head>
<body>
    @yield('content')
</body>
</html

Alright, this will look exactly like this in the source code. Let's make a another template which extends this.

// home.blade.php
@extends('master')

@section('content')
    <h1>Welcome</h1>
    <p>This is my homepage</p>
@endsection

This will first of inherit the parent, and replace @yield('content') with:

<h1>Welcome</h1>        <p>This is my homepage</p>

Is there any explanation at all why this happens? For longer sub-templates reading the source code is a living hell. Best way to see the "source code" is to see the generated one in inspect element, which also is just the live code, and not the first generated.

- Sligthly annoyed developer

1

1 Answers

1
votes

I believe this is up to Laravels way of handling views. Every blade-view you create in resources/views will be "translated" to PHP. This line in your code for example:

@yield('content')

will be translated to

<?php echo $__env->yieldContent('content'); ?>

you can easely check for yourself by checking all files at storage/framework/views/. This are the files Laravel will include to build the "real" HTML website. Note, that all tab-stops made in .blade.php views are replaced with 4 spaces. And there are this ugly intendations.