dthl8036 2016-10-19 09:30
浏览 30
已采纳

如何将数据传递到我在Laravel 5.3中创建的模板文件

How do I pass the $styles and $scripts variables to my template view file from IndexController that calls a child view? My current code will only send the data to child view.

class BaseController extends Controller
{
    public $styles;
    public $scripts;

    public function __construct()
    {
        // Set styles and scripts
        $this->styles = array(
            // css files go here
        );
        $this->scripts = array(
            // js files go here
        );
    }
}

class IndexController extends BaseController
{
    // protected vars here

    public function __construct(
        // interface files go here
    )
    {
        // vars here

        // Append styles and scripts to existing parent array
        parent::__construct();

        $this->styles['css goes here'] = 'screen';

        $this->scripts[] = 'js goes here';
    }

    public function index()
    {
        View::make('index')->with('styles', $styles)
                ->with('scripts', $scripts)
    }
}

UPDATE: Added my view files

Template view file

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="index, follow" />
<title>Title</title>
@foreach($styles as $file => $type)

    <link href="{{ $file}}" media="{{ $type }}" rel="stylesheet">

@endforeach
@foreach($scripts as $file)

    <script src="{{ $file }}"></script>

@endforeach
</head>
<body>
<div id="content">
    {{ $content }}
</div>
</body>
</html>

Child view file

@extends('layout.template')

@section('content')
    // html for child view
@endsection
  • 写回答

1条回答 默认 最新

  • dougong7850 2016-10-19 09:41
    关注

    You can pass your variables as an array through to your blade template by adding it to the data parameter:

       public function index()
        {
            $styles = $this->styles;
            $scripts = $this->scripts;
    
            return view('index', compact($styles, $scripts))
        }
    

    EDIT

    I'm not sure whether you have just shortened your code to make your question more understandable, but here are some of the issues I can see with the code you posted:

    Lack of $this in index():

    Your $styles and $scripts variables need to be $this->styles and $this->scripts to get the class variables.

    Not returning a view in index():

    your index function isn't returning anything, you need to add return to return the view:

    public function index()
    {
        return View::make('index')->with('styles', $this->styles)
                ->with('scripts', $this->scripts)
    }
    

    Yielding content with a variable:

    <div id="content">
        {{ $content }}
    </div>
    

    I can't see a $content variable being passed to the view, maybe it's in a view composer somewhere? but the normal way to add content is to use yield which injects the content section in to your view:

    <div id="content">
        @yield('content')
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP