doujia1988
doujia1988
2014-04-27 15:49

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

已采纳

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 duanhu7390 7年前

    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
    
    点赞 评论 复制链接分享