You are not logged in.
(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">+</div>
</div>
</div>
</div>
<input type="button" id="create" name="create" value="Create">
</div>
</div>
@endsectionError 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
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
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
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
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 ... ![]()
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
[Answered]
You are absolutely right, this fixed it! thank you so much!
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
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
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.
-- Phill Sparks ( Code | Bundles )
Offline
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
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