Laravel4 Blade template issue: extends method is printed as a string - php

I am new to Laravel4, so I was learning from the documentation.
It seems everything is all correct, but the extends method literally displays its code on my web browser. I don't know hwy..
This is the layout file (base.blade.php under layouts directory)
<!-- app/views/layouts/base.blade.php -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<!-- You can provide some default content -->
#section('head')
<link rel="stylesheet" href="style.css" />
#show
</head>
<body>
#yield('body')
</body>
</html>
and home.blade.php.
<!-- app/views/home.blade.php -->
#extends('layouts.base')
#section('body')
<h1>Hurray!</h1>
<p>We have a template!</p>
#stop
and my routing configuration is like below..
Route::get('/',
function()
{
return View::make('home');
}
);
Now when you access to the URL, it displays just a string!
#extends('layouts.base')
I have been googling for a hour or so! still could not fix it...
What's wrong with them? :(
I checked common mistakes below
possible type
incorrect path
incorrect method name
missing appending 'blade' on the file name
BUT STILL CAN'T FIND THE PROBLEM. :(

There shouldn't be any space in the template above #extends at all no comments or whitespace

Remove
<!-- app/views/home.blade.php --> from home.blade.php
home.blade.php
#extends('layouts.base')
#section('body')
<h1>Hurray!</h1>
<p>We have a template!</p>
#stop

Possibilities are that you have printed #extends('layouts.base') twice in the page. Please have a look.

Related

Ok, my #yield or #extends is not working because some reason

I just wanna print something thanks to #yield in my layout (cristarium), but for some reason is doing nothing, it just appear the text MASTER but don't HELLO.
VIEWS FOLDER LOCATIONS
resources\views\ffxiii-2\cristarium.blade.php
resources\views\ffxiii-2\cristarium_personajes\noel_kreiss.blade.php
ROUTE IN WEB.PHP
Route::get('/ffxiii-2/cristarium', function () {
return view('ffxiii-2.cristarium');
})->name('cristarium');
LAYOUT -> cristarium.blade.php
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Cristarium</title>
</head>
<body>
MASTER
<div class="container">
#yield('content')
</div>
</body>
</html>
BLADE TO PRINT -> noel_kreiss.blade.php
#extends('ffxiii-2.cristarium')
#section('content')
HELLO
#endsection
I tried to change the name's folder avoiding the underscore, and changing to the - in ffxiii-2, but still don't work, so I think that's not the problem.
You are returning the layout file in your controller method. But I think you should return the second file (noel_kreiss.blade.php):
Route::get('/ffxiii-2/cristarium', function () {
return view('ffxiii-2.cristarium_personajes.noel_kreiss');
})->name('cristarium');
Views cascade the other way.
Your route is showing the view which renders the file ffxiii/cristarium.blade.php,
this is your master layout.
What you need to do is to return the view ffxiii-2/cristarium_personajes/noel_kreiss from the route, which will the render that view, which in turn includes your master layout.
E.g.
Route::get('/ffxiii-2/cristarium', function () {
return view('ffxiii-2.cristarium_personajes.noel_kreiss');
})->name('noel_kreiss');

Laravel use yield for separated layouts on different folder

in resources folder and layouts/backend/ and i have main.blade.php file as master template and partials folder which that have some files such as sidebar, footer and etc, for example:
/layouts/backend/main.blade.php:
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
</head>
<body>
<!-- Footer -->
#yield('partials.footer')
<!-- /footer -->
</body>
</html>
/layouts/backend/partials/footer.blade.php:
#extends('layouts.backend')
#section('footer')
<div class="footer text-muted">Hello</div>
#endsection
after save and refreshing page i don't have footer on page
Have a look at the Docs, the section about Blade is really good.
I think there are a few logical issues here. If you have a footer that you want to include in your backend/master template, then there's no need to extend the layout in your footer, simply include it in your master template. Here's an example:
layouts/backend/main.blade.php:
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
</head>
<body>
#yield('main')
<!-- Footer -->
#include('layouts.backend.partials.footer')
<!-- /footer -->
</body>
</html>
layouts/backend/partials/footer.blade.php:
<div class="footer text-muted">Hello</div>
Also, you might want to review your folder organisation, maybe you can go with something similar
views/layouts/backend.blade.php
views/layouts/frontend.blade.php
views/layouts/backend/footer.blade.php
views/layouts/backend/header.blade.php
This is just an idea, of course you can do the folder organisation as you prefer.

#yield placed at wrong place

I'm new to Laravel, and I'm trying to do a "modular" page for the first time. Everything was going fine, had my base layouts, which gets extended on my home page, have set up some sections/yields with no problem (content, title, etc) but one specific #yield keeps being rendered at the wrong place, I've put it inside my head file (head.blade.php), which already have another #yield for the title, but that one keeps getting rendered inside the body. I tried doing some tests, and discovered that if I put my title #yield inside <title></title> it works OK, but if I put it outside the tag it is moved to the body.
Thats a normal Laravel way of working (#yield can't be by itself, only inside a tag) or something is wrong ?
default.blade.php
<!doctype html>
<html>
<head>
#include('includes.head')
</head>
<body>
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--no-desktop-drawer-button">
#include('includes.nav')
#yield('tools')
<main class="mdl-layout__content">
<div class="page-content">
#yield('content')
</div>
</main>
#include('includes.footer')
</div>
</body>
</html>
head.blade.php
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="cache-control" content="no-cache">
<meta name="description" content="XXXXXX">
<meta name="author" content="XXXXXXXXXXXXX">
<title>#yield('title')</title> =====> Works normally if put here
#yield('title') =====> Rendered inside the body if put that way
<!-- jQuery 3.2.1 -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<!-- Normalize CSS -->
<link rel="stylesheet" type="text/css" href="css/normalize.css">
<!-- Dialog Polyfill -->
<link rel="stylesheet" type="text/css" href="css/dialog-polyfill.css">
<!-- Bootstrap 3.3.7 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
It's a normal browser behaviour. First of all you have to place all the things inside the right places. It's like you are trying eat some fat meat without fork and then crying that your hands in fat :)
You always have to define all tags that browsers expect. Title tag define that inside title of the page is located. yield('title') just means the name that you gave its for inserting there then. It looks like id in HTML.
Since you haven't described to browser what do you want to insert here it is trying to solve the problem and it's usually a placement all the things without needed tags in body (only if we are talking about omitting tags in the header).

different stylesheet for different pages in laravel

I'm new to laravel. I have two pages A and B which uses tables. Now in my layout.blade.php I have a link towards only one stylesheet which is affecting both the tables in A and B. I only want it in A and for B I have different set of stylesheet. How do I target the stylesheet to the pages?
You can load css files directly in these views, so A would load one css and B will load another one.
Another way if to use conditional loading in layout.blade.php:
#if (request()->is('pageA'))
// Load CSS for A
#elseif (request()->is('pageB'))
// Load CSS for B
#endif
Usually in this case you should have a master layout that only include global scripts and stylesheets, says layouts/global.blade.php
<!doctype html>
<html lang="en-US">
<head>
<title>Your website</title>
#section('meta')
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
#show
#section('stylesheets')
<link rel="shortcut icon" href="{{asset("favicon.ico")}}" type="image/x-icon"/>
<link type="text/css" rel="stylesheet" href="{{asset("css/global.css")}}"/>
#show
#section('scripts')
<script src="{{url("js/global.js")}}"></script>
#show
</head>
<body>
#yield('content')
</body>
</html>
Then in your page_a.blade.php, you extend it and include your own specific stylesheets:
#extends('layouts/global')
#section('stylesheets')
#parent
<link type="text/css" rel="stylesheet" href="{{url("css/page_a.css")}}"/>
#endsection
#section('scripts')
#parent
<script src="{{url("js/page_a.js")}}"></script>
#endsection
#section('content')
Your page A content
#endsection
I think you may create a section like the following in your view, for example:
#extends('layouts.master')
#section('styles')
<link href="{{asset('assets/css/custom-style.css')}}" />
#stop
Hope this helps you.
You can use sass css for that conditions. In that sass css you can use if and else condition and check variable.
Please refer below link for how to use it.
http://thesassway.com/intermediate/if-for-each-while

"//" on top of every html page using laravel 5 blade layout

I'm creating web application using laravel 5. Everypage has "//" on up-left corner. What is causing this?
The app.blade.php looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
#yield("content")
<p>Above content generated by MVC</p>
</div>
</div>
</body>
</html>
Well it certainly isn't anything in the blade template that's doing it.
What is probably happening is that you've got somewhere in your code a line which says echo "//"; or something similar, or a rogue line of code before your <?php block starts -- maybe you were trying to comment out a block of code that includes a <?php block.
That line doesn't have to be in the template; it could be anywhere in the code; if it's run before the template is output, then you will get the kind of effect that you're reporting here.
As for where the line is and what it's doing there, that's something you'll have to work out for yourself. But you can start by searching your codebase for echo or print statements, and for //<?php.

Categories