Framework: @php bug -- Inline/Multiline

Created on 26 Sep 2017  路  11Comments  路  Source: laravel/framework

  • Laravel Version: 5.5.13
  • PHP Version: 7.2
  • Database Driver & Version: MySQL v5.7.19

Description:

Using the @php directive inline followed by a multiline results in the compiled view not rendering properly.

Steps To Reproduce:

@php($var = 1)
<div>
  Stuff In Here
</div>
@php
  $new_var = 2;
@endphp

Most helpful comment

Yep, for the easy copy pasta solutions, I added this to the boot method of my app serviceprovider:

\Blade::directive('raw', function ($expression) {
    return "<?php {$expression}; ?>";
});

And replaced @php( with @raw( in my code using a quick search and replace.

All 11 comments

Same here, the @php($var = 1) is not documented... wonder if it was supposed to be supported at all (https://laravel.com/docs/5.4/blade#php).

Maybe similar to #20994

From what I can tell you cannot format your php code this way with blade.
As you see here: https://github.com/laravel/framework/blob/9d0f23deed323710fa8b7c57b502f8c07b2b61ba/src/Illuminate/View/Compilers/BladeCompiler.php#L217

The compiler looks for any instance of @php then parses out the php code with a regular expression (e.g. everything between @php and @endphp)

I believe this is a request for a new feature, not a bug.

This worked in 5.4 so that is where the confussion is, if it should have worked is another thing :)

https://github.com/laravel/framework/blob/5.4/src/Illuminate/View/Compilers/Concerns/CompilesRawPhp.php#L15

But yeah it does seem it's no longer supported, would be very handy to have mentioned in the upgrade guide.

If @php($var = 1) no longer supported in 5.5, I have to update many places in the blade :)

@stayallive @ShahidH That's exactly the issue. It used to work, and 5.5 broke it without mentioning it in the upgrade docs. I'm not sure why it shouldn't be a feature... It's convenient to do an inline php tag, for quick variables.

No one realized it was broken until after 5.5 was released ( see #20994) and was marked as a no-fix. That's the problem with all these blade helpers that get added. They are limited by how laravel parses blade and are not useful in many use-cases. Best to not use the helpers outside the most basic use-case.

If this is no longer supported you can still use Custom Laravel Directives in order to bring this functionality back

Yep, for the easy copy pasta solutions, I added this to the boot method of my app serviceprovider:

\Blade::directive('raw', function ($expression) {
    return "<?php {$expression}; ?>";
});

And replaced @php( with @raw( in my code using a quick search and replace.

Since it is no longer supported I think we can close this issue and make a PR to the upgrade guide/docs

It has been added to the Upgrade Guide here: https://github.com/laravel/docs/blame/5.5/upgrade.md#L338

Can be closed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shopblocks picture shopblocks  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

felixsanz picture felixsanz  路  3Comments

progmars picture progmars  路  3Comments