Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel blade include error - syntax error, unexpected

Tags:

php

laravel

blade

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'

like image 683
Peter Estiven Avatar asked Apr 17 '14 06:04

Peter Estiven


People also ask

How do I fix parse error syntax error unexpected?

How To Fix parse error syntax error unexpected ' ' in wordpress Via FTP? In order to fix the Syntax Error in WordPress you need to edit the code that caused this error. The only possible way to resolve the syntax error is to directly exchange the faulty code via FTP or access the file you last edited using FTP.

What is syntax error unexpected?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

What is T_string?

Fixing "Parse error: syntax error Unexpected T_STRING error" This type of error (syntax error) usually happens because of a typo in your code, such as a missing apostrophe/quotation mark/a missing or extra space, or code that has been commented out incorrectly.

What is parse error syntax error?

A parse error: syntax error, unexpected appears when the PHP interpreter detects a missing element. Most of the time, it is caused by a missing curly bracket “}”. To solve this, it will require you to scan the entire file to find the source of the error.


3 Answers

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

like image 76
majidarif Avatar answered Oct 18 '22 09:10

majidarif


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 // <--
like image 45
The Alpha Avatar answered Oct 18 '22 07:10

The Alpha


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.

like image 28
Tash Pemhiwa Avatar answered Oct 18 '22 07:10

Tash Pemhiwa