laravel blade include error - syntax error, unexpe

2019-04-28 08:48发布

I'm using Laravel 4 locally with EasyPHP 14.1. I have created a route :

Route::get('/test', function()
{
    return View::make('test');
});

a layout (testlayout.blade.php) :

<!doctype html>
<html>
<head>
    <title></title>
</head>
<body>
    <div class="container">
        @yield('container')
    </div>
</body>
</html>

and a view (test.blade.php) :

@extends("testlayout")
@section('container')
    <h1>Hello!</h1>
@stop

It works fine and I get "Hello!"

But when a change my layout by adding an include like this :

<!doctype html>
<html>
<head>
    <title></title>
</head>
<body>
    <div class="container">
        @yield('container')
    </div>
        @include('testfooter')
</body>
</html>

with my footer (testfooter.blade.php) :

@section("testfooter")
    <div class="footer">2013-2014</div>
@show

I have an error "syntax error, unexpected '/'".

The code generated by Laravel (found in app\storage\views) is wrong :

<!doctype html>
<html>
<head>
    <title></title>
</head>
<body>
    <div class="container">
        <?php echo $__env->yieldContent('container')
    </div>
        <?php echo $__env->make('testfooter', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>; ?>
</body>
</html>

Laravel forgets ;?> after yieldContent('container') and adds two ;?> after ... render(). I've tried many things but I really don't have any clue now.

UPDATE1 I have reinstalled a fresh Laravel and guest what, the problem is still the same. Without @include it's ok, with an @include I have the same error again and again.

UPDATE2 and PROBLEM FIXED (thanks to @majimboo)! This is just unbelievable. All that story is not related to the code. This is a problem with the text editor : Notepad++ on Windows. For an unknown reason, and for somes files only, it switched from 'edit/EOL conversion/windows format' to 'edit/EOL conversion/Mac format'. And apparently, Laravel doesn't Mac format at all! So, if you use Notepad++ on Windows, be sure that you have : 'EOL conversion/windows format'

4条回答
Juvenile、少年°
2楼-- · 2019-04-28 09:07

Usually Laravel gives a great hint to the problem. The problem might be in your CSS or JS include statements. First make sure you have turn on the debug mode of laravel. Here is how to do it.

  1. Config/app -> debug = true at line 16
  2. Refresh the page where the error is

I have intentionally leave out closing ')' of css file. The laravel FW showed where that error, and it will show you similar error.

Hope it help.

查看更多
仙女界的扛把子
3楼-- · 2019-04-28 09:16

In my case the issue was an unterminated string in some PHP in a totally different part of the file than Laravel was complaining about. I eliminated it by removing everything in the file and adding the lines in again one by one.

查看更多
趁早两清
4楼-- · 2019-04-28 09:25

the problem is in the file that is getting included.

First to check if the problem is really that, remove everything inside testfooter.blade.php then open the view from the browser. You'll notice that there is no error anymore.

Change your testfooter.blade.php to:

<div class="footer">2013-2014</div>

remove the @section("testfooter")...@show.

Example:

<!-- app/views/header.blade.php -->

<h1>When does the Narwhal bacon?</h1>

<!-- app/views/footer.blade.php -->

<small>Information provided based on research as of 3rd May '13.</small>

You include it like:

<!-- app/views/example.blade.php -->

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Narwhals</title>
</head>
<body>
    @include('header')
    <p>Why, the Narhwal surely bacons at midnight, my good sir!</p>
    @include('footer')
</body>
</html>

Check this to learn more on blade.


The problem seems to be something else, I would just like to share this, If it can help you:

I'm using Notepad++ as text-editor and for some strange reason it had decided to use "MAC format" as the End-Of-Line (EOL) format. Apparently the Blade framework can't cope with that. Use the conversion function (in notepad++ : Edit -> EOL Conversion) to convert to Windows Format and it will work just fine...

Arjen

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-04-28 09:30

You have this:

@section("testfooter")
    <div class="footer">2013-2014</div>
@show // <--

Change it to this:

@section("testfooter")
    <div class="footer">2013-2014</div>
@stop // <--
查看更多
登录 后发表回答