Support the ongoing development of Laravel.io →
Input Views Blade
Last updated 6 months ago.
0

Hey,

Change return View::make('start', $content); to return View::make('start', compact('content', 'navi'));.
They will end up in your view as $content and $navi

Alternatively you can tinker around with return View::make('start', array('content' => $content, 'navi' => $navi));

Last updated 6 months ago.
0

I do it like this

class BaseController extends Controller {
    protected $data = array();
}

class IndexController extends BaseController {

    public function showHello() {
        $navi = array("Test", "Test2");
        $content = array($navi, 'Test');

        $this->data = array(
               'navi'        => $navi,
               'content'   => $content
          );
        

        return View::make('start', $this->data);
    }

}
Last updated 6 months ago.
0

This an idea?
(Use what works for your purposes)

// Base Controller

<?php

class BaseController extends Controller
{

    protected $layout = "templates.master";

    protected $data;

    public function __construct()
    {
        $this->layout->navi = array("Test", "Test2");
        // $this->layout->navi = View::make('templates.nav');
    }

    /**
     * Setup the layout used by the controller.
     *
     * @return void
     */
    protected function setupLayout()
    {
        if (!is_null($this->layout)) {
            $this->layout = View::make($this->layout);
        }
    }

}

// Controller

<?php

class IndexController extends BaseController {

    public function showHello()
    {
        $data = MyDatabaseModel::all(); // Or any other source
        $this->layout->content View::make('start', compact('data'));
    }

}

// templates.master

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <nav>
        @yield('nav')
        {{-- Or @include('templates.nav') if it's a static navigation --}}
    </nav>
    @yield('content', 'Nothing to load.')
</body>
</html>

// start (template)

@section('content')
    {{ var_dump($data) }}
@stop
Last updated 6 months ago.
0
$data = array('navi' => $navi, 'content' => $content);
return View::make('start', $data);
Last updated 6 months ago.
0

I made Zenries code and it doesnt works too O.o My code of the IndexController.php looks now like this:

<?php

    class IndexController extends BaseController {

	    public function showHello()
	    {
            $navi = array("Test", "Test2");
            $content = 'Test';
            $data = array('navi' => $navi, 'content' => $content);
            return View::make('start', $data);
	    }

    }
    ?>

My start.blade.php looks like this:

@extends('layout')

@section('content')
    {{ $data['content'] }}
@section('navi')
    {{ $data['navi'] }}
@stop

And my layout.blade.php looks like this:

<!DOCTYPE html>
<html>
  <head>
    <title>Hallo!</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="../css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js";></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js";></script>
    <![endif]-->
  </head>
  <body>
        <div class="container content">
            <ul>
                @yield('navi')
            </ul>
            @yield('content')
        </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://code.jquery.com/jquery.js";></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="../js/bootstrap.min.js"></script>
  </body>
</html>

Error can be seen here Klick me harder

Thanks for your Time and help ;)

Last updated 6 months ago.
0

if you're using zenry's code try this in start.blade.php

@extends('layout')

@section('content')
    {{ $content }} //instead of $data['content']
@section('navi')
    {{ $navi }} //instead of $data['navi']
@stop
Last updated 6 months ago.
0

Even with your code it wont work -.- Here is the error:

Undefined variable: data (View: /var/www/tim/app/views/start.blade.php)

Last updated 6 months ago.
0

But you shouldn't try to access $data in your view.

Make something like this in your controller

$data = array(
'foo1' => 'bar1',
'foo2' => 'bar2'
)
return View::make('start', $data);

Then in your View you cannot use $data variable but $foo1 and $foo2 which will have "bar1" and "bar2" values accordingly.

Last updated 6 months ago.
0

It works - Thanks ;)

Last updated 6 months ago.
0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

pasternt pasternt Joined 7 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.