Framework: blade php tag doesn't get commented

Created on 9 Oct 2017  路  16Comments  路  Source: laravel/framework

  • Laravel Version: 5.5
  • PHP Version: 7.0.18
  • Database Driver & Version: mysql

Hi,
I have this code that is commented by {{-- --}} tags:

{{-- 
            @php
            $i=-1;
            @endphp
            @foreach($mostLikedProducts as $instagramMedia)
                @php
                $i++;
                $arrayPrice = price_format($instagramMedia->products->price);
                $price = $arrayPrice['num'];
                $priceUnit = $arrayPrice['unit'];
                @endphp
                <section class="section-padding cl popular-product">
                    <div class="col-14 content-wrap">
                        <strong class="text-primary">{{ $instagramMedia->like }} {{ trans('instagram.likes') }}</strong>
                        <a href="{{ url(productUrl($instagramMedia->products))}}" class="" title="{{$instagramMedia->products->title}}" >
                            <h2 class="no-side-margin text-xl">{{ $instagramMedia->products->title }}</h2>
                        </a>
                        <p class="opacity-75 text-md">{{ $instagramMedia->products->description }}</p><br><br>

                    </div><!--  /.figcaption -->
                    @if($i % 2 != 0)
                        <div class="img-wrap col-10">
                            <img src="{{ $instagramMedia->products->media->link }}" alt="{{ $instagramMedia->products->title }}">
                        </div>
                    @endif
                </section>
                <hr>
            @endforeach
 --}}

But the part in php tags doesn't get comment and this is the error:
image

Line 538 those codes are in php tags also $i=-1 is in php tags that is commented.

Most helpful comment

Sorting this tonight

All 16 comments

If i'm not mistaken, the comments only work inline. So I don't see this as a bug. You can close the issue

@Dylan-DPC I used this syntax before in laravel 5.2 and has not any problem, Right now I have lot's of this commenting syntax in other places but not with @php @endphp tags in multiple lines and everything is just fine!
yesterday I upgrated laravel to 5.5 and this problem showed up.

Can you show us the full file? I can't replicate this with the code you've provided. Also what exact version of Laravel 5.5 are you using?

@ntzm it's 5.5.14 :) and about file content it's too much anyway here it is:
https://pastebin.com/uqBhjBXy

Search echo $instagramMedia->products->title; it's in line 543 I commented this but just between php tags show up and execute in the view!

Also I changed Laravel version to 5.5.0 problem still exists!

You've commented out a section where you loop over $mostLikeProducts. Further down there is a section that loops through $mostPopularTagProducts.

The snippet you showed clearly indicates that you're looping through $mostPopularTagProducts and not $mostLikedProducts.

In other words, the comments are working just fine (compiled the view myself to confirm) and the problem lies in your code, not the framework.

This is the kind of thing that tends to happen when you have over 1000 lines in a single blade file.

Ya as @36864 (why no name man :P ) pointed out this isn't a bug and the issue can be closed.

@36864 Thanks, But you are mistaken!
When I remove the commented section it works fine every thing works fine, Also when I uncomment that section this works fine codes have no problem!
The problem is that when blade compile this view just the part in @php tags doesn't get commented other @for @if and html files are commented and not showed up!
if you search for echo $instagramMedia->products->title; that error point at this line you can see that this part is just in one line the line in @php tags that is commented so when this is commented why it should be in compiled view?

if you search for echo $instagramMedia->products->title; that error point at this line you can see that this part

In your original post you showed an exception being thrown at $arrayPrice = price_format($instagramMedia->products->title);. The snippet you showed from your error page does not include the echo statement you just mentioned.

Now, you haven't shared what exception is being thrown, so it's hard to debug this further.

Taking a second look at your code and your issue, I can't find the exact code you say you've commented in the pastebin you posted. A search for $i=-1 comes up empty.

If you can, please present a minimal test case so we don't have to parse through a thousand lines of code trying to find something that isn't even actually there. If not, at the very least, please post the actual code you're using.

Ok here is a small test case I have this code:
image

And when I run the page this error will appear:
image

As You see I have commented the first php tags with foreach and html tags but php tags doesn't get commented and executed so because of foreach is commented and we haven't a variable named $key error happens.

Alright, I'm able to reproduce now. Here's a minimal test case:

{{--
@php
$foo = $bar;
@endphp
--}}
@php
@endphp

This compiles to

<?php
$foo = $bar;
?>

Workaround: add a character (or more) to the @php directive so it doesn't get parsed.

{{--
@php--{comment_workaround}
$foo = $bar;
@endphp
--}}
@php
@endphp

This compiles to

<?php--{comment_workaround}
foo = $bar;
?>

which doesn't get parsed by php.

Note that this workaround does leak code into the compiled html. Browsers should ignore it, but it will be presend in the source.

I would advise just removing your dead code from your view files instead of commenting things out.

Sorting this tonight

As the PR got closed, the chances of this bug getting fixed is low :stuck_out_tongue:

@ntzm Thanks,
I like to remove unnecessary codes but sometimes they should be commented for just a period of time so this isn't a good idea :)

Closing as a no-fix. There are a few things blade can't just do right, in that case I suggest that you use plain PHP.

@HassanShojaei as a safer workaround, comment your code inside the php tags as well or, instead of comments, wrap your unused code in an always-false if block.

{{--
@php
/*
  $foo = $bar;
*/
@endphp
--}}
@php
  $i = 0;
@endphp

or

@if(false)
@php
  $foo = $bar;
@endphp
@endif
@php
  $i = 0;
@endphp
Was this page helpful?
0 / 5 - 0 ratings

Related issues

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

ghost picture ghost  路  3Comments

JamborJan picture JamborJan  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

YannPl picture YannPl  路  3Comments