Laravel 的@yield 遇到一个很坑爹的问题。。。

2014-08-04 01:13:50 +08:00
 konakona
按照教程上的指示这么做着(http://v4.golaravel.com/docs/4.2/templates#blade-templating)

就是在系统自动生成的HomeController含有如下代码:

protected $layout = 'layouts.master';

/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}


public function showWelcome()
{
// echo $content = View::make('hello'); //一段html内容
$this->layout->title = '我是标题';
$this->layout->content = View::make('hello');
// echo $this->layout->content; //一段html代码
}

----------
这里说明下hello.php模板中的内容,就是一段已经写好了的bootstrap前端框架代码,单独在showWelcome()方法中使用View::make('hello')方法获取后echo出有了这个模板。
在echo $this->layout->content时也有内容!


在blade中,我写@yield('content','暂时没有内容...') 却每次都显示“暂时没有内容”。悲剧啊~~~~~~~~






------------------

刚刚在layout中进行了如下代码测试:

<div class="container">
<?php echo $content; //出现了html内容 ?>
@yield('content')<!--无内容-->
</div>

为什么@yield不显示内容?
6549 次点击
所在节点    程序员
9 条回复
levn
2014-08-04 02:36:07 +08:00
yield是用来显示section的
@section('content')
<p>This is my body content.</p>
@stop
@yield('content', 'Default Content');

你这里的content只是个绑定变量
{{ $content or 'Default Content' }}
konakona
2014-08-04 04:33:20 +08:00
@levn @section('content') 这个我也试过了,木有反应。
WildCat
2014-08-04 06:47:05 +08:00
@konakona
php artisan cache:clear
试试
cooper
2014-08-04 08:05:53 +08:00
小提示:

hello.php 改为 hello.blade.php
WildCat
2014-08-04 08:16:19 +08:00
@cooper 仔细看了楼主的输出,确实是这个问题!
trimleo
2014-08-04 12:01:01 +08:00
blade后缀的才会被laravel的模板引擎解析 hello.php模板改成hello.blade.php
konakona
2014-08-04 22:46:02 +08:00
@WildCat
@trimleo
@cooper
感谢!
但是出现了一个新的问题。
我将hello.php改名为hello.blade.php后,hello.blade.php的内容比layouts/master.blade.php早出现。
konakona
2014-08-04 22:50:51 +08:00
问题已经解决。~~
cooper
2014-08-05 08:06:50 +08:00
团哥 你是夜猫子啊。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/125959

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX