I'm trying to understand laravel's basic blade template engine, but I can't seem to get past a basic example. My blade template is not loading .It only show the white screen but when I remove the hello.blade.php to hello.php it works .Any suggestion?
Routes.php
Route::get('/', 'PagesController#home');
PagesController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PagesController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function home()
{
return Views('hello');
}
}
hello.blade.php
<html>
<head>
<title>Hello World</title>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Starting to learn Laravel 5</div>
</div>
</div>
</body>
</html>
There is no Views helper. It's called view:
return view('hello');
Well, I had the same "white screen problem". And the blade docs in laravel is so confused. Then, try this:
Create a layout, lets say template.blade.php:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Starting to learn Laravel 5</div>
#yield('view_content')
</div>
</div>
</body>
Create a simple view that you want to wrap into the template, lets say hello.blade.php:
#extends('template')
#section('view_content')
This is my view content
#stop
Now, in your controller just call the view instead the layout:
public function home(){
return view('hello');
}
I'm not sure how it is working regardless of the extension as in your controller the syntax is incorrect. You return the view not Views... return view('hello'). You should technically see something like the following:
FatalErrorException in PagesController.php line 18:
Call to undefined function App\Http\Controllers\Views()
Even if app_debug is false you should see Whoops, looks like something went wrong.
I got this when I made a typo like so:
return views('abc');
instead of
return view('abc');
That extra s was killing everything.
Related
I have a newly started project in Laravel 8.0, and I have a problem with views.
I have the following master.blade.
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
<div class="container">
#yield('content')
</div>
<footer class="row">
#include('layouts.footer')
</footer>
</body>
</html>
I then have a view that extends that master blade:
#extends('layouts.app')
#extends('layouts.master')
#section('content')
HELLO
#stop
The problem is, when rendering the view, the information is duplicated. Appears once inside the container div so the #yield works, but is re-rendered in another div outside the container (main class="py-4").
Let's see if someone can help me with the problem.
Thank you very much in advance.
#extends('layouts.master')
#section('content')
HELLO
#endsection
you extends two time
You must add one extends, edit the code and do the code below
#extends('layouts.master')
#section('content','your title')
HELLO
#endsection
that the right way to get the view.
I am trying to make layout using blade but the problem is that when i tried to
#yield on the file which is included in master file but #yield is not working.
resouces/views/layouts/app.blade.php
<html>
<head>
...
...
</head>
<body>
#include('layouts.navigation')
#include('layouts.main_panel')
#include('layouts.footer')
</body>
</html>
resouces/views/layouts/main_panel.blade.php
// some html stuff
#yield('form')
// some html stuff
resouces/views/auth/login.blade.php
#extends('layouts.app')
#section('form')
<form>
// input
</form>
#endsection
I would suggest you pass variables to the partials and echo
them inside it. This is an alternative way to achieve what you are
trying to do.
For example -
Partial blade file (resouces/views/partials.header.blade.php) -
<h4>{{ $name }}</h4>
View (resouces/views/custom.blade.php) -
#include('partials.header', [ 'name' => 'Lorem Ipsum' ])
I am also using laravel framework but i used to do in that way :-
Layout :- resouces/views/layouts/app.blade.php
<html>
<head>
...
...
</head>
<body>
#include('layouts.navigation')
#yield('content') // use #yield here why you need separate file
#include('layouts.footer')
</body>
</html>
After that :- resouces/views/auth/login.blade.php
#extends('layouts.app')
#section('content')
<form>
// input
</form>
#stop
Hope it helps!.. I used to follow this structure in laravel project
I've already installed Laravel Blade Highlighter..
#yield #section #endsection is not working, just a plain white text..
My views are working properly.. I can browser them okay..
Here's my code:
web.php:
Route::get('/', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
pagesController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function index(){
return view('pages.index');;
}
public function about(){
return view('pages.about');
}
public function services(){
return view('pages.services');
}
}
app.blade.php
<h1>This is the laravel</h1>
#yield('content')
index.php:
#extends('layouts.app')
#section('content')
<h1>Welcome to Laravel</h1>
<p>this is a content</p>
#endsection
The answer is I should close all open tab on my sublime text..
And index.php, about.php and services.php should be index.blade.php, about.blade.php and services.blade.php
This may seem like an odd question, but I am new to Laravel and have a lot of trouble finding the right syntax in the official documentation. Here is an example. I was using the following blade syntax to try and have a conditional view:
#if (condition)
#extends 'layouts.regular'
#else
#extends 'layouts.special'
#endif
This did not work. I then went to the blade documentation on the Laravel site and read through the Extending a Layout section. All seemed well and I spent 10 minutes debugging my code and trying to find out if I did something wrong. It all seemed correct, so I then resorted to Google and found this thread.
From the comments I understood that:
The #extends statement must be the first line in a blade template
You can only use a conditional extend by using a ternary expression
While this solved my problem, it took too long to find. I still can't figure out WHERE in the official documentation this is mentioned. Am I using the official documentation wrong? Is there other, more extensive documentation out there? I keep feeling that the online documentation is very brief and often does not explain important details on syntax or exceptions...
#extends always comes in the first line of blade, it is used to define the layout e.g.
Assume we have master.blade.php
<html>
<head>
</head>
<body>
#yield('content')
</body>
</html>
Then we have page.blade.php
#extends('master') // this one says this page will be placed in master.blade.php
#section('content') // find the line that says #yield('content') and replace it with...
<h1> Hello World </h1>
#stop
Now, if you have many layouts and you want to share the content with it, you can achieve that programatically away from blade.. take this example
Assume we have master.blade.php
<html>
<head>
<title>Master</title<
</head>
<body>
#yield('content')
</body>
</html>
and we have master2.blade.php
<html>
<head>
<title>Master 2</title>
</head>
<body>
#yield('content')
</body>
</html>
and we have page.blade.php
#section('content')
<h1>Hello World</h1>
#stop
// note that we have no #extends()
now in the controller we do something like this
if($cond)
{
$layout = view('master');
}
else
{
$layout = view('master2');
}
$page = view('page');
$layout->content = $page;
return $layout;
Note that we are using $layout->content because both layouts are yielding content in #yield('content')
Hi all I have problame with my form I am writing html form but its error
and this is my route code
Route::get('course','CourseController#index');
and this is my CourseController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CourseController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('course.create');
}
and this is my view course/create.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<div class="container">
{!! Form::open(array('route' => 'course.store')) !!}
<input type="text"><br>
<input type="password"><br>
{!! Form::close() !!}
</div>
</body>
</html>
and error:
Whoops, looks like something went wrong.
1/1 FatalErrorException in 818f720d4e075893d8198d2b0ff02e25 line 9: Class 'Form' not found
Help me please
With Laravel 5.* the Form & Html have been deprecated. Check out uprade guide (search Form & HTML Helpers).
1. Install laravelcollective/html
composer require laravelcollective/html
2. Add the Form and HTML facades and service provider.
Edit config/app.php and add this line to the 'providers' array:
'Collective\Html\HtmlServiceProvider',
Next, add these lines to the 'aliases' array:
'Form' => 'Collective\Html\FormFacade',
'Html' => 'Collective\Html\HtmlFacade',