刀片条件语句 [英] Blade conditional statements

查看:80
本文介绍了刀片条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几种型号.每个模型都有一个创建和编辑页面.如果尚未创建模型,则需要显示其创建页面.如果已创建,则需要显示其编辑页面.

I have several models. Each model has a create and edit page. If the model has not been created yet, I need to display its create page. If it has been created, I need to show its edit page.

到目前为止,我有这个可行的方法

So far I have this which works

<div class="panel-group" id="accordion">
    @if (is_null($project->projectType))
        @include ('projects.partials.projectTypesCreate')
    @else
        @include ('projects.partials.projectTypesEdit')
    @endif

    @if (is_null($project->projectData))
        @include ('projects.partials.projectDataCreate')
    @else
        @include ('projects.partials.projectDataEdit')
    @endif

    @if (is_null($project->projectTesting))
        @include ('projects.partials.projectTestingCreate')
    @else
        @include ('projects.partials.projectTestingEdit')
    @endif
</div>

在我看来,对于许多有条件的声明来说,以上内容并不是最漂亮的.更糟糕的是,我需要按顺序显示事物.我的意思是,如果projectType为null, 仅应显示projectTypesCreate.如果不为null.应该显示projectTypesEdit和projectDataCreate,依此类推.

Now, the above is not the prettiest, to many conditional statements in my opinion. What makes this worse is the fact that I need to display things in order. By this I mean that if projectType is null, only projectTypesCreate should be displayed. If it is not null. projectTypesEdit and projectDataCreate should be displayed, and so on.

要做到这一点,我可能会做类似的事情

To accomplish this I could potentially do something like so

@if (is_null($project->projectType))
    @include ('projects.partials.campaignTypes')
@else
    @include ('projects.partials.campaignTypesEdit')
@endif

@if (!is_null($project->projectType) && is_null($project->projectData))
    @include ('projects.partials.projectDataCreate')
@else
    @include ('projects.partials.projectDataEdit')
@endif

@if (!is_null($project->projectData) && is_null($project->projectTesting))
    @include ('projects.partials.projectTestingCreate')
@else
    @include ('projects.partials.projectTestingEdit')
@endif

再一次,它变得有点笨拙,并且我所需的信息也不准确,因为条件失败时将显示编辑页面.

Once again though, it is getting a bit clunky and it is not accurate as to what I need because the edit page will display when the condition fails.

因此,实际上我只是在寻求一些有关如何最好地处理这种情况的建议.我需要确定要为每种模型显示的视图,同时要顺序显示视图.

So really I am just after some advice as to how to best handle this situation. I need to determine what view to display for each model, whilst at the same time displaying views sequentially.

谢谢

推荐答案

创建一个单独的$action变量,该变量保存要执行的操作,然后在@include语句中使用该变量:

Create a single $action variable that holds the action you want to do, then use that in your @include statements:

<div class="panel-group" id="accordion">
    @php($action = is_null($project->projectType) ? 'Create' : 'Edit')

    @include('projects.partials.projectTypes'.$action)
    @include('projects.partials.projectData'.$action)
    @include('projects.partials.projectTesting'.$action)
</div>

这篇关于刀片条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆