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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?