php Laravel Blade 包含错误 - 语法错误,意外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23125662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel blade include error - syntax error, unexpected
提问by Peter Estiven
I'm using Laravel 4 locally with EasyPHP 14.1. I have created a route :
我在本地使用带有 EasyPHP 14.1 的 Laravel 4。我创建了一条路线:
Route::get('/test', function()
{
return View::make('test');
});
a layout (testlayout.blade.php) :
一个布局(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) :
和一个视图(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) :
我的页脚(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 :
Laravel 生成的代码(在 app\storage\views 中找到)是错误的:
<!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.
Laravel 忘记;?>
afteryieldContent('container')
并添加两个;?>
after ... render()
。我已经尝试了很多东西,但我现在真的没有任何线索。
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.
UPDATE1 我已经重新安装了一个新的Laravel和guest什么的,问题还是一样。没有@include 没关系,有@include 我一次又一次地犯同样的错误。
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'
UPDATE2 和问题已修复(感谢@majimboo)!这简直令人难以置信。所有这些故事都与代码无关。这是文本编辑器的问题:Windows 上的 Notepad++。由于未知原因,仅对于某些文件,它从“编辑/EOL 转换/windows 格式”切换到“编辑/EOL 转换/Mac 格式”。显然,Laravel 根本不支持 Mac 格式!因此,如果您在 Windows 上使用 Notepad++,请确保您具有:' EOL 转换/Windows 格式'
采纳答案by majidarif
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.
首先检查问题是否真的存在,删除里面的所有内容,testfooter.blade.php
然后从浏览器打开视图。你会注意到没有错误了。
Change your testfooter.blade.php
to:
将您更改testfooter.blade.php
为:
<div class="footer">2013-2014</div>
remove the @section("testfooter")
...@show
.
删除@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 thisto 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...
我使用 Notepad++ 作为文本编辑器,出于某种奇怪的原因,它决定使用“MAC 格式”作为行尾 (EOL) 格式。显然,Blade 框架无法应对这种情况。使用转换功能(在记事本++中:编辑 - > EOL转换)转换为Windows格式,它会工作得很好......
回答by The Alpha
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 // <--
回答by Haroon Ali Shah
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.
通常 Laravel 会对问题给出很好的提示。问题可能出在您的 CSS 或 JS 包含语句中。首先确保你打开了laravel的调试模式。这是如何做到的。
- Config/app -> debug = true at line 16
- Refresh the page where the error is
- Config/app -> debug = true 在第 16 行
- 刷新出现错误的页面
I have intentionally leave out closing ')' of css file. The laravel FW showed where that error, and it will show you similar error.
我故意省略了关闭 css 文件的 ')'。Laravel FW 显示了该错误的位置,它会向您显示类似的错误。
Hope it help.
希望有帮助。
回答by Tash Pemhiwa
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.
在我的情况下,问题是与 Laravel 抱怨的文件完全不同的部分 PHP 中的未终止字符串。我通过删除文件中的所有内容并逐行添加行来消除它。