0

I've been hunting around google and stack overflow, but I can not find anyone with this issue. As far as I can tell, I can't use Blade variables {{ $likethis }} or I get a rendering issue in the layout I'm creating. Everything except double bracket variables works with layouts, etc. Here is some code:

The variable being passed is 'title'.

<?php 

class Add_Controller extends Base_Controller {
    public $restful = true;

    public function get_recipe(){
        return View::make('add.recipe', array('title' => 'Add A Recipe'));
        // I've also tried with() and others.
    }
}

The layout:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>{{ $title }}</title>
    <link rel="stylesheet" href="{{ URL::to('css/style.css') }}">
    <link href='http://fonts.googleapis.com/css?family=Libre+Baskerville|Domine|Donegal+One' rel='stylesheet' type='text/css'>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="{{ URL::to('js/functions.js') }}"></script>
</head>
<body>
    @yield('content')
</body>
</html>

recipe page:

@layout('layouts.default')
@section('content')
    <div id="wrapper">
        <header><div id="icon"></div></header>
        <div id="create">
            <div class="inline">
                <input type="text" id="title" name="title" value="Title">
            </div>
            <div>
                <label for="serving">Servings</label>
                <input type="text" name="serving" id="serving" value="2">
            </div>

            <div id="ingredients">
                <div class="ingredient">
                    <div class="inline">
                        <input type="text" class="name" name="name" value="Ingredient">
                    </div>
                    <div class="inline">
                        <input type="text" class="amt" name="amount" value="0">
                    </div>
                    <div class="inline">
                        <select name="unit" class="unit">
                            <option value="lb">Pounds</option>
                            <option value="oz">Ounces</option>
                        </select>
                    </div>
                    <div class="inline">
                        <div class="add-ingr">&#43</div>
                    </div>
                </div>
            </div>
            <input type="button" id="create" name="create" value="Create">
        </div>
    </div>
@endsection

Error I get:

Unhandled Exception
Message:

Error rendering view: [layouts.default]

Undefined variable: title

Location:

/home/daevskii/public_html/websites/sbr/storage/views/9e4501d6d0479b217301c46cfb8cbcfd on line 5

Stack Trace:

#0 /home/daevskii/public_html/websites/sbr/laravel/laravel.php(42): Laravel\Error::native(8, 'Undefined varia...', '/home/daevskii/...', 5)
#1 /home/daevskii/public_html/websites/sbr/laravel/view.php(386) : eval()'d code(5): Laravel\{closure}(8, 'Undefined varia...', '/home/daevskii/...', 5, Array)
#2 /home/daevskii/public_html/websites/sbr/laravel/view.php(386): eval()
#3 /home/daevskii/public_html/websites/sbr/laravel/blade.php(71): Laravel\View->get()
#4 [internal function]: Laravel\{closure}(Object(Laravel\View))
#5 /home/daevskii/public_html/websites/sbr/laravel/event.php(199): call_user_func_array(Object(Closure), Array)
#6 /home/daevskii/public_html/websites/sbr/laravel/event.php(138): Laravel\Event::fire('laravel.view.en...', Array, true)
#7 /home/daevskii/public_html/websites/sbr/laravel/view.php(348): Laravel\Event::until('laravel.view.en...', Array)
#8 /home/daevskii/public_html/websites/sbr/laravel/view.php(386) : eval()'d code(36): Laravel\View->render()
#9 /home/daevskii/public_html/websites/sbr/laravel/view.php(386): eval()
#10 /home/daevskii/public_html/websites/sbr/laravel/blade.php(71): Laravel\View->get()
#11 [internal function]: Laravel\{closure}(Object(Laravel\View))
#12 /home/daevskii/public_html/websites/sbr/laravel/event.php(199): call_user_func_array(Object(Closure), Array)
#13 /home/daevskii/public_html/websites/sbr/laravel/event.php(138): Laravel\Event::fire('laravel.view.en...', Array, true)
#14 /home/daevskii/public_html/websites/sbr/laravel/view.php(348): Laravel\Event::until('laravel.view.en...', Array)
#15 /home/daevskii/public_html/websites/sbr/laravel/view.php(590): Laravel\View->render()
#16 /home/daevskii/public_html/websites/sbr/laravel/response.php(246): Laravel\View->__toString()
#17 /home/daevskii/public_html/websites/sbr/laravel/laravel.php(180): Laravel\Response->render()
#18 /home/daevskii/public_html/websites/sbr/public/index.php(34): require('/home/daevskii/...')
#19 {main}

* @package Laravel * @version 3.2.12

If I take out {{ $title }}, I get no error... but then I'm also not using variables :-\ Thanks in advance!!

[ANSWER] I had a route issue that was not posted here, but the answer is in this laravel forum post I made: http://forums.laravel.com/viewtopic.php?pid=23383#p23383

6
  • 1
    What's the filename (and path) of the blade layout you show above? Commented Jan 1, 2013 at 3:40
  • Good question; application > views > layouts > default.blade.php Commented Jan 1, 2013 at 3:52
  • Variables from nested views aren't pass to layouts. Commented Jan 1, 2013 at 4:00
  • I don't know about that itachi, See my comment to Javier below. This seems like it works, but it not working for me. Commented Jan 1, 2013 at 4:16
  • To set the title in the layout try $this->layout->title ='something'; Commented Jan 1, 2013 at 6:32

3 Answers 3

3

The problem is elsewhere - not in your code posted above. I just copied and pasted your entire code into a fresh install of Laravel 3.2.12 - and it works correctly;

Screenshot

So therefore - if I was you - I would re-download the current Laravel 3.2.12 and see if you can copy and paste the code above to make it work. If it does work, then you other 'install' is bugged somehow. If it does not work, then there is something wrong with your development server.

Sign up to request clarification or add additional context in comments.

3 Comments

THANK you. I knew someone would finally discover my code was right... Now the puzzle solving can begin. First, I am using 3.2.12, I pasted it at the end of the post. I will try the re-install of 3.2.12.
New install has same results. Can anyone think up what might cause this?
It was a route issue... and my fault. I've updated my post with the answer and will mark you as correct!
0

I believe variables you pass are only for that view not the layout. Look into view composers if you want to pass variables to your templates.

2 Comments

This video by andrew perkins shows it working, unless I'm missing something. Check video from 3:34 - 4:22(ish) youtube.com/watch?v=lceNwQf-ufY&list=PL09BB956FCFB5C5FD
Sure thing, I added before the error paste. Note that I'm using sections.. I tried {{content}} in the layout, but same issue (with necessary changes in other places)...
0

You will need to use $this->layout->title ='something'; in your controller before returning the view.

1 Comment

The problem is not in the code, check TheShiftExchange's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.