laravel Blade 中的条件扩展

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

Conditional extends in Blade

phpajaxlaravelblade

提问by Jordan Doyle

Is there any way to do a conditional @extendsstatement in the Blade templating language?

有没有办法@extends在 Blade 模板语言中做一个条件语句?

What I've tried:

我试过的:

@if(!Request::ajax())
    @extends('dashboard.master')
    @section('content')
@endif

<div class="jumbotron">
    Hey!
</div>

@if(!Request::ajax())
    @stop
@endif

Output

输出

When the request was not AJAX it printed out @extends('dashboard.master'), but the AJAX request worked fine.

当请求不是 AJAX 时,它会打印出来@extends('dashboard.master'),但 AJAX 请求工作正常。

What I'm trying to do

我想做什么

Stop including the master template (which includes headerand footer) for AJAX so it can easily display the requested content

停止包含AJAX的主模板(包括headerfooter),以便它可以轻松显示请求的内容

采纳答案by itachi

in the master layout:

在主布局中:

   @if(!Request::ajax())

       //the master layout with @yield('content'). i.e. your current layout

   @else

       @yield('content')

   @endif

回答by Christopher Raymond

@extends((( Request::ajax()) ? 'layouts.ajax' : 'layouts.default' ))

回答by Adam Lavin

This kind of logic should really be kept out of the template.

这种逻辑真的应该被排除在模板之外。

In your controller set the $layoutproperty to be dashboard.master then instead of calling returning your view or response, terminate with just $this->layout->content = View::make('dashboard.template')

在您的控制器中将$layout属性设置为dashboard.master 然后而不是调用返回您的视图或响应,只需终止$this->layout->content = View::make('dashboard.template')

Take a look at the Laravel docson this

看看关于这个的Laravel 文档

You could end up with something like this

你可能会得到这样的结果

<?php

class Something extends BaseController {

    $layout = 'dashboard.master';

    public function getIndex()
    {
        $template = View::make('dashboard.template');

        if(Request::ajax()) {
            return $template;
        }

        $this->layout->content = $template;
    }
}