laravel 刀片模板中的意外 endforeach

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24184925/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:38:00  来源:igfitidea点击:

unexpected endforeach in blade template

laravelforeachblade

提问by Jerodev

I have a bladetemplate in laravelin laravel that displays a tab view. The tabs are different eventtypesand in the tabs are the eventsper type. This view worked fine until today. The only thing that changed is that I made it possible to soft-delete events.

我在 laravel 中有一个显示选项卡视图的blade模板laravel。选项卡是不同的eventtypes,选项卡中是events每种类型。直到今天,这种观点仍然有效。唯一改变的是我可以软删除events.

The error I get is the following:

我得到的错误如下:

syntax error, unexpected 'endforeach' (T_ENDFOREACH) 

This is the entire source code of the template:

这是模板的整个源代码:

@extends('master')

@section('title', '- Kalenderbeheer')

@section('head')
{{ HTML::script('Script/lib/dataTables.js'); }}
<script>
    $(document).ready(function(){
        $("#EventTypeTabs").tab();

        $(".eventtable").dataTable({
            paging: false,
            info: false,
            "aoColumns": [
                null,
                null,
                null,
                { "asSorting": [] }
            ]
        });

        $(".eventtable tr").click(function(){
            if ($(this).data("id"))
                location.href = "/kalender/admin/" + $(this).data("id");
        })
    });
</script>
@stop

@section('body')
<div class="row">
    <div class="col-md-6 col-md-offset-1">
        <h1>Kalenderbeheer</h1>
    </div>
    <div class="col-md-4 rtl">
        <a href="/kalender/admin/nieuw">Nieuwe activiteit toevoegen</a>
    </div>
</div>
<div class="row">
    <div class="col-md-10 col-md-offset-1">
        <ul class="nav nav-tabs" style="margin-bottom: 15px;" id="EventTypeTabs">
            @foreach ($eventtypes as $et)
            <li @if($et->id == 1) class="active" @endif>
                <a href="#{{{ $et->getFormattedName() }}}"  data-toggle="tab">{{{ $et->name }}}</a>
            </li>
            @endforeach
        </ul>
        <div class="tab-content eventtypetabs">
            @foreach ($eventtypes as $et)
            <div class="tab-pane fade @if ($et->id == 1)active in@endif" id="{{{ $et->getFormattedName() }}}">
                @if (count($et->events) > 0)
                <table class="table table-striped eventtable">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>Datum</th>
                        <th>Naam</th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php $c = 0; ?>
                    @foreach ($et->events as $event)
                    <tr data-id="{{ $event->id }}">
                        <td>{{ ++$c }}</td>
                        <td>{{ preg_replace("/\d{2}:\d{2}:\d{2}/", "", $event->start) }}</td>
                        <td>{{{ $event->name }}}</td>
                        <td>X</td>
                    </tr>
                    @endforeach
                    </tbody>
                </table>
                @endif
            </div>
            @endforeach
        </div>
    </div>
</div>
@stop

The error appears on the last @endforeach. This is a screenshot of the error: laravel error

错误出现在最后一个@endforeach. 这是错误的屏幕截图: 错误

回答by Razor

You just need to add a space before @endif (line 48)

您只需要在@endif 前添加一个空格(第 48 行)

<div class="tab-pane fade @if ($et->id == 1)active in @endif" id="{{{ $et->getFormattedName() }}}">

Also, it's better to use the ternary operator:

此外,最好使用三元运算符:

class="tab-pane fade {{ $et->id == 1 ? 'active in' :''}}"