doujia1988 2014-04-27 15:49
浏览 54
已采纳

控制器布局 - 如何使用修改的内容成功返回布局

I'm trying to set and return a layout that's attached to a controller. I can successfully set the content for @yeild('content') but I cannot both set and return the template. I can either return the template, with no content set, or I can return the set content, with no layout template.

master.blade.php

<html>
    <head>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

        @yield('styles')

    </head>
    <body>
        <div class="container">
            <h1>Google Earth project!</h1>
            <a href="http://laravel.com/docs/quick">Saucey sauce for laravel</a>
            <hr>
            <h4>Content</h4>

            @yield('content')

        </div>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

        @yield('scripts')

    </body>
</html>

HotspotController.php

...
public function show($uid, $lid, $hid)
{
    // $this->layout->content = View::make('hotspot.profile');
    $this->layout->content = 'a string';
    return $this->layout;
}
....
  • 写回答

1条回答 默认 最新

  • duanhu7390 2014-04-27 16:04
    关注

    There is apparently a difference between @yield('content') and {{ $content }}.

    In order to accomplish what you're trying to accomplish you will have to use the latter way of declaring variable content on the blade template.

    HotspotController.php

    protected $layout = 'layouts.master';
    ...
    public function show($uid, $lid, $hid)
    {
        $this->layout->content = View::make('hotspot.profile');
    }
    ...
    

    hotspot.profile.blade.php

    I am a hotspot profile
    

    master.blade.php

    <html>
        <head>
            <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    
            @yield('styles')
    
        </head>
        <body>
            <div class="container">
                <h1>Google Earth project!</h1>
                <a href="http://laravel.com/docs/quick">Saucey sauce for laravel</a>
                <hr>
                <h4>Content</h4>
    
                {{ $content }}
    
            </div>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    
            @yield('scripts')
    
        </body>
    </html>
    

    Alternatively...

    You can keep @yield('content') in your master template, but you will have to modify hotspot.profile.blade.php to incorporate @section('content') @stop. See hotspot.blade.php

    master.blade.php

    <html>
        <head>
            <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    
            @yield('styles')
    
        </head>
        <body>
            <div class="container">
                <h1>Google Earth project!</h1>
                <a href="http://laravel.com/docs/quick">Saucey sauce for laravel</a>
                <hr>
                <h4>Content</h4>
    
                @yield('content')
    
            </div>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    
            @yield('scripts')
    
        </body>
    </html>
    

    hotspot.profile.blade.php

    @section('content')
    I am a hotspot profile
    @stop
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?