1
votes

So I'm getting a weird result that I can't wrap my head around. It's just a matter of how pretty the html look in the source, but it's a bit annoying as it also indent inconsistently sometimes.

Here is the layout.blade.php file, ignoring most of the html above the section:

            </div>
            <div class="content">
                @yield('content')
            </div>
        </div>
    </body>
</html>

Here is the index.blade.php that extends the layout.blade.php:

@extends('layout')   

@section('content')

<div class="main-title">
    <h1>Developer</h1>
    <svg>
        <line x1="0" y1="0" x2="300" y2="0" style="stroke:rgb(255,255,255);stroke-width:10" />
    </svg>
    <h1>Designer</h1>
</div>

@stop

I would assume this would be put at the same indent level as @yield() as that is what is happening in django template, and laravel tutorials.

This is what I am getting instead:

            <div class="content">

<div class="main-title">
    <h1>Developer</h1>
    <svg>
        <line x1="0" y1="0" x2="300" y2="0" style="stroke:rgb(255,255,255);stroke-width:10" />
    </svg>
    <h1>Designer</h1>
</div>

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

As you can see the content isn't places where the @yield() was placed in the layout.blade.php.

Looking at the generated php file that is being served It looks like it should be fine:

            </div>
            <div class="content">
                <?php echo $__env->yieldContent('content'); ?>
            </div>
        </div>
    </body>
</html>

Generated from index.blade.php:

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

                <div class="main-title">
                    <h1>Developer</h1>
                    <svg>
                        <line x1="0" y1="0" x2="300" y2="0" style="stroke:rgb(255,255,255);stroke-width:10" />
                    </svg>
                    <h1>Designer</h1>
                </div>

<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout', \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render(); ?>

Everything about this leads me the assume the served html would have propper tab spacing, but it doesen't. What can be the reason? Could there be some configuration or is it just a quirk of blade?

2
I'm trying to figure this one out myself now! lol I'm wondering if it's output is based on it's indention in the index blade file? See if it changes based off that maybe? Also did you mean index.blade.html or index.blade.php?Das
That was a mistake, index.blade.php is the correct file.Andreas Halvorsen Tollånes
Why do you expect the framework to insert tabs for you? You ask it to insert the section and it does.miken32
I suppose you can use an after middleware to look at the response and use tidy_parse_string() to clean it up. I would recommend not doing this on a production site though. php.net/manual/en/tidy.parsestring.phpuser1669496

2 Answers

1
votes

What can be the reason? Could there be some configuration or is it just a quirk of blade?

The reason is: That's how PHP works and how Laravel blade as a construction on top of PHP can achieve what it does. The directive @yield('content') is a function call: echo $__env->yieldContent('content');. You have already guessed it. Here the implementation.

Now the point is that the function takes 'content' as input parameter. As for how functions work in PHP (and most imperative programming languages), it is not aware of the preceding spaces that you have put as indentation, it simply cannot consume it as a parameter by using that syntax. Consider that if this example actually worked as you expected, this would imply a violation on PHP syntax and semantic, and many other things that now work would suddenly stop working, because the language itself needs to keep syntax constructions consistency.

Sure, what you state is correct in the way that it might look "a bit like an annoying quirk" from the developer or user perspective, but that was never stated that blade or PHP should work that way, meaning that it's not an expectable feature.

Now, if your intention is to get altered output code, there are many ways to apply filters, both for minification/obfuscation or prettifying, like middleware filters. Of course, those approaches may require some processing power that you will evaluate from your needs and environment (local or prd).

0
votes

I was bothered by this as well, and as this will not be fixed in Laravel itself I decided to fix this in a package that extends on the blade compiler. The regex that finds the blade syntax in templates now includes any horizontal whitespace leading up to the blade statement, and passes it on to the resulting PHP statement that renders the content. After any newline in the replaced content the spacing is then added, and if the replaced content doesn't have a trailing newline it is added as well. Hope this helps anyone that comes across this!