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

        $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>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="index, follow" />
@foreach($styles as $file => $type)

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

@foreach($scripts as $file)

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

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

Child view file


    // html for child view
  • 写回答

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))


    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 }}

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



  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值
  • ¥20 数据排序,可选择排序方向
  • ¥15 修改一下代码,考虑进程到达时间不同的情况
  • ¥15 帮我看看这是个啥题,带解题过程和结果,条件如下FCF = 290471.33 g1 = 15% r = 8% g2 = 4% n = 5
  • ¥15 edem模拟颗粒不显示或者生成失败
  • ¥15 Python代码编写
  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞