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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料