V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
konakona
V2EX  ›  程序员

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

  •  
  •   konakona ·
    54853315 · 2014-08-04 01:13:50 +08:00 · 6546 次点击
    这是一个创建于 3553 天前的主题,其中的信息可能已经有所发展或是发生改变。
    按照教程上的指示这么做着(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不显示内容?
    9 条回复    2014-08-05 08:06:50 +08:00
    levn
        1
    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
        2
    konakona  
    OP
       2014-08-04 04:33:20 +08:00
    @levn @section('content') 这个我也试过了,木有反应。
    WildCat
        3
    WildCat  
       2014-08-04 06:47:05 +08:00
    @konakona
    php artisan cache:clear
    试试
    cooper
        4
    cooper  
       2014-08-04 08:05:53 +08:00   ❤️ 1
    小提示:

    hello.php 改为 hello.blade.php
    WildCat
        5
    WildCat  
       2014-08-04 08:16:19 +08:00
    @cooper 仔细看了楼主的输出,确实是这个问题!
    trimleo
        6
    trimleo  
       2014-08-04 12:01:01 +08:00
    blade后缀的才会被laravel的模板引擎解析 hello.php模板改成hello.blade.php
    konakona
        7
    konakona  
    OP
       2014-08-04 22:46:02 +08:00
    @WildCat
    @trimleo
    @cooper
    感谢!
    但是出现了一个新的问题。
    我将hello.php改名为hello.blade.php后,hello.blade.php的内容比layouts/master.blade.php早出现。
    konakona
        8
    konakona  
    OP
       2014-08-04 22:50:51 +08:00
    问题已经解决。~~
    cooper
        9
    cooper  
       2014-08-05 08:06:50 +08:00
    团哥 你是夜猫子啊。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5407 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:58 · PVG 14:58 · LAX 23:58 · JFK 02:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.