Laravel 4:如果刀片布局中的语句工作异常 [英] Laravel 4: if statement in blade layout works strange

查看:68
本文介绍了Laravel 4:如果刀片布局中的语句工作异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下为什么如果我正常请求页面(而不是ajax)时出现带有打印字符串"@extends('layouts.default')"的黑屏吗?

Could someone explain me why I get blank screen with printed string "@extends('layouts.default')" if I request page normally (not ajax)?

@if(!Request::ajax())
  @extends('layouts.default') 
  @section('content')
@endif
Test
@if(!Request::ajax())
  @stop
@endif

我正在尝试解决Ajax的问题​​,我不想为每种请求类型创建2个模板,并且我也不想使用刀片模板,因此使用控制器布局对我不起作用.如何在刀片模板中执行此操作?我正在看这个 Laravel:如何仅渲染一个模板的一部分?

I'm trying to solve problem with Ajax, I don't want to create 2 templates for each request type and also I do want to use blade templates, so using controller layouts doesn't work for me. How can I do it in blade template? I was looking at this Laravel: how to render only one section of a template?

顺便说一句.如果我使用ajax请求它,它应该会按预期运行.

推荐答案

是的@extends必须在第1行上.

Yes @extends has to be on line 1.

然后我找到了PJAX的解决方案.起初,我不确定这是否可以解决我的问题,但确实可以.不知道为什么如果您实际上不能以这种方式丢失刀片功能,我为什么会害怕丢失它.如果有人正在使用PJAX,并且需要使用一个带有和不带有布局的模板,那么这可能是您的解决方案:

And I found solution for PJAX. At the beginning I was not sure this could solve my problem but it did. Don't know why I was afraid to lose blade functionality if you actually can't lose it this way. If someone is using PJAX and needs to use one template with and without layout this could be your solution:

protected $layout = 'layouts.default';

public function index()
{
  if(Request::header('X-PJAX'))
  {
    return $view = View::make('home.index')
      ->with('title',  'index');
  }
  else
  {
    $this->layout->title = 'index';
    $this->layout->content = View::make('home.index');
  }
}

这篇关于Laravel 4:如果刀片布局中的语句工作异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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