You are not logged in.

Important Note: This forum is being archived and will be completely locked down on the 7th of February. The new forum can be found at http://laravel.io.

Announcement

    Laravel 4 Advanced Architecture: Book Now On Sale

#1 2013-01-01 16:39:08

daevski
Artisan
Registered: 2013-01-01
Posts: 26

Unable to render views with variables [stumped]

(Answered below, thanks to anggietp :: http://forums.laravel.io/viewtopic.php?pid=23383#p23383 )

I've confirmed with the help of a StackOverflow user that my code is correct, but something in my environment is not correct. (stackoverflow com /questions/14107615/laravel-unable-to-render-variables)

I'm not sure where to begin puzzle solving, but maybe a kind developer or savvy user can help?
If I take out {{ $title }}, I get no error... but then I'm also not using variables :-\ Thanks in advance!!

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

Last edited by daevski (2013-01-09 04:47:09)

Offline

#2 2013-01-01 17:05:56

lewiscains
Artisan
Registered: 2012-12-27
Posts: 5

Re: Unable to render views with variables [stumped]

Hi Daevski,

Have you tried doing it this way:
-------------------------------------------------------   
public function get_recipe(){
        $data = array('title' => 'Add A Recipe');
        return View::make('add.recipe', $data);
       
}
-------------------------------------------------------

This works fine for me so I hope it helps you as well. If this doesn't work perhaps your routes file is incorrect? May be worth posting that as well :-)
Good luck,
Lew

Offline

#3 2013-01-01 17:10:08

daevski
Artisan
Registered: 2013-01-01
Posts: 26

Re: Unable to render views with variables [stumped]

lewiscains wrote:

public function get_recipe(){
        $data = array('title' => 'Add A Recipe');
        return View::make('add.recipe', $data);
       
}

I've just tried this, but it didn't help.
Note that if I remove {{$title}} it renders fine, so it's not a route issue, but my route is simply:

Route::get('/add/recipe', function()
{
	return View::make('add.recipe');
});

Offline

#4 2013-01-01 17:15:20

daevski
Artisan
Registered: 2013-01-01
Posts: 26

Re: Unable to render views with variables [stumped]

I don't know if it's related, but I took out the line under it:

<link rel="stylesheet" href="{{ URL::to('css/style.css') }}">

And tried to use an Asset instead:

	<title>Plain Title</title>
	<?php echo Asset::styles(); ?>

after adding this to base controller:

<?php

class Base_Controller extends Controller {

	public function __construct()
	{
	    //Assets
	    // Asset::add('jquery', 'js/jquery-1.7.2.min.js');
	    Asset::add('style', 'css/style.css');
	    parent::__construct();
	}

	/**
	 * Catch-all method for requests that can't be matched.
	 *
	 * @param  string    $method
	 * @param  array     $parameters
	 * @return Response
	 */
	public function __call($method, $parameters)
	{
		return Response::error('404');
	}

}

But the Asset code seems to do absolutely nothing (page source):

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>Plain Title</title>
	
</head>
<body>
		<div id="wrapper">
		<header><div id="icon"></div></header>
... 
...

It just seems to leave whitespace in the page source! :-\

Last edited by daevski (2013-01-01 17:16:51)

Offline

#5 2013-01-01 17:22:18

anggietp
Artisan
From: Indonesia
Registered: 2012-12-26
Posts: 29
Website

Re: Unable to render views with variables [stumped]

daevski wrote:

Note that if I remove {{$title}} it renders fine, so it's not a route issue, but my route is simply

Route::get('/add/recipe', function()
{
	return View::make('add.recipe');
});

You mentioned before, you access it from controller ... smile
That's wont work, and definitely route problem.

Why you make route to return View? You said you had a controller right?

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

So you should just do this (if you still wanna using route):

Route::get('add/recipe','add@recipe');

// or just register it because your controller is straightforward (same as route)
Route::controller('add');

How much lines of code which you wrote today? ;)

Offline

#6 2013-01-01 17:28:40

daevski
Artisan
Registered: 2013-01-01
Posts: 26

Re: Unable to render views with variables [stumped]

[Answered]
You are absolutely right, this fixed it! thank you so much!

anggietp wrote:

So you should just do this (if you still wanna using route):

Route::get('add/recipe','add@recipe');

// or just register it because your controller is straightforward (same as route)
Route::controller('add');

Offline

#7 2013-01-01 17:30:45

lewiscains
Artisan
Registered: 2012-12-27
Posts: 5

Re: Unable to render views with variables [stumped]

Hi again,
I'm pretty sure the parent construct needs to be above the assets so hopefully that should help you:

       public function __construct()
    {
            parent::__construct();
             //Assets
        // Asset::add('jquery', 'js/jquery-1.7.2.min.js');
        Asset::add('style', 'css/style.css'); 
    }

With your other problem I agree with that as well. If you link to your controller from your route and call the view from there (passing in your variables) you shouldn't have a problem.
Hope this works for you.

Offline

#8 2013-01-01 21:39:03

Phill Sparks
Quality Team
From: Leicester, UK
Registered: 2011-09-29
Posts: 1,480
Website

Re: Unable to render views with variables [stumped]

View::make('add.recipe', array('title' => 'Add A Recipe'));

Your code will pass $title to add.recipe.  It does not pass $title to your layout.  For this you either need to add another section to your view for the title, and yield that in your layout, or you could use View::share(string $key, mixed $value) to make $title available to all views.

Offline

#9 2013-01-01 23:31:36

misterR
Apprentice
From: Lille, france
Registered: 2013-01-01
Posts: 4

Re: Unable to render views with variables [stumped]

Controller

     public function get_recipe(){
        Section::inject('title','Add A Recipe');
        return View::make('add.recipe');
    }

the layout

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>@yield('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>

i' m french laravel lover ;)

Offline

#10 2013-01-01 23:33:33

anggietp
Artisan
From: Indonesia
Registered: 2012-12-26
Posts: 29
Website

Re: Unable to render views with variables [stumped]

Phill Sparks wrote:
View::make('add.recipe', array('title' => 'Add A Recipe'));

Your code will pass $title to add.recipe.  It does not pass $title to your layout.  For this you either need to add another section to your view for the title, and yield that in your layout, or you could use View::share(string $key, mixed $value) to make $title available to all views.

But why he succeeded? I also tried that and it worked. Variable title will be available in base layout, even we just call view make add.recipe...(using blade)


How much lines of code which you wrote today? ;)

Offline

Board footer

Powered by FluxBB

');