Laravel #yield is undefined for HTML section - php

The layout/template file is defined in footer.blade.php:
#extends('mainpage')
#section('footer')
<section class="section5">
<footer class="container col-lg-12 pb-2 pt-4">
<ul class="nav justify-content-center border-bottom pb-3 mb-3">
<li class="nav-item">Home</li>
<li class="nav-item">Features</li>
<li class="nav-item">Pricing</li>
<li class="nav-item">FAQs</li>
<li class="nav-item">About</li>
</ul>
<div class="row">
<p class="small-text col-lg-3 text-lg-start text-center">© MyCompany.com #php echo date("Y") #endphp </p>
<p class="small-text col-lg-6 text-lg-center text-center">All trademarks used on this site are the property of their respective owners.</p>
<p class="small-text col-lg-3 text-lg-end text-center">Terms & Conditions | Privacy Policy</p>
</div>
</footer>
</section>
#endsection
This is then yielded in mainpage.blade.php:
<!-- REST OF HTML -->
#yield('footer', 'Undefined')
</body>
Both files exist in the same directory, and there is also a route for /mainpage defined in web.php:
Route::get('/', function () {
return View::make('mainpage')->render();
});
Yet the only thing that navigating to /mainpage shows me is the Undefined "error" that I passed to it as the second argument to the #yield helper. Inspecting in the browser shows that the section isn't loaded.
Because the template inheritance fails silently without an error and because Laravel apparently doesn't even have an intuitive way of printing warnings, such a basic use case seems impossible for me to debug.
What's going on here, or alternatively, how would I begin to go about debugging it?

For your current view to work, your route would need to call footer instead of mainpage.
footer.blade.php knows what view it extends.
<!-- footer.blade.php -->
#extends('mainpage') <!-- Ok, extend mainpage.blade.php -->
#section('footer') <!-- place this section where there's a #yield('footer') in mainpage.blade.php -->
#endsection
mainpage.blade.php doesn't know what views will extend it.
<!-- mainpage.blade.php -->
#yield('footer', 'Undefined') <!-- Place 'Undefined' if the views that extend me don't have a #section('footer') -->
I could make a second view called footer2.blade.php, and also have it extend mainpage. In that situation, which view should mainpage render?
What you want for generic sections like footers is including a subview (using #include).
Here's a crude example:
<!-- layout-1.blade.php. This layout has a navbar, sidebar and footer. -->
<html>
<head>
</head>
<body>
<!-- layout-1 navbar -->
#include('navbar')
<!-- layout-1 sidebar -->
#include('sidebar')
<main>
#yield('content')
</main>
<!-- layout-1 footer -->
#include('footer')
</body>
</html>
<!-- layout-2.blade.php. This layout has a navbar and a footer but no sidebar -->
<html>
<head>
</head>
<body>
<!-- layout-2 navbar -->
#include('navbar')
<main>
#yield('content')
</main>
<!-- layout-2 footer -->
#include('footer')
</body>
</html>
#extends('layout-1')
#section('content')
#endsection
#extends('layout-2')
#section('content')
#endsection

Related

Laravel yield Content not showing

I am new in laravel, and I am trying to create layout, but my #yield is not working when I view in browser its shows everything, but it doesn't show any thing from dashboard.blade.php
dashboard.blade.php
#extends('layouts.main')
#section('content')
<div class="page-title">
<h1>Hello World</h1>
</div>
#endsection
main.blade.php
#include('includes.header')
<!-- page content -->
<div class="right_col" role="main">
<div class="">
#yield('content')
</div>
</div>
<!-- /page content -->
includes/header.blade.php
<!DOCTYPE html>
<html>
<head>
<!-- My CSS Links -->
</head>
web.php
Route::get('/dashboard','DashboardController#index');
DashboardController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller{
public function index(){
return view('admin.layouts');
}
}
Thanks in advance :).

Navigate with routes (laravel blade.php)

Hi there so I am trying to construct a navbar that will change the information being displayed.
NOTE using laravel 4
The idea is to have one page where you enter information into a form and then another where you can then edit that form data but i cant get the bloody thing to navigate.
My directory is set up as such
- app
-- views
--- layouts
------- default.blade.php
--- pages
------- home.blade.php
------- edit.blade.php
--- includes
------- head.blade.php
------- header.blade.php
------- footer.blade.php
And I would like to use something similar to this to navigate
<div class="navbar">
<div class="navbar-inner">
<a id="logo" href="/">ABC123</a>
<ul class="nav">
<li>Home</a></li>
<li>Edit</li>
</ul>
</div>
</div>
Currently my routes.php file looks like this
Route::get('/', function()
{
return View::make('pages.home');
});
Route::get('edit', function()
{
return View::make('pages.edit');
});
and here is my default.blade.php template that I want to use for both
<!doctype html>
<html>
<head>
<title>#yield('title')</title>
#include('includes.head')
</head>
<body>
<div class="container">
<header class="row">
#include('includes.header')
</header>
<div id="main" class="row">
#yield('content')
</div>
<footer class="row">
#include('includes.footer')
</footer>
</div>
</body>
</html>
Thanks for any help
Have a great day!
What you need is the url() helper:
<ul class="nav">
<li>Home</a></li>
<li>Edit</li>
</ul>

Laravel #section

My view folder structure
content
home.blade.php
includes
partials
mainsection.blade.php
layout
default.blade.php
My home.blade.php
#extends('layout.default')
#section('content')
#section('partials.mainsection')
<div class="col-xs-10 col-xs-offset-1 col-md-8 col-md-offset-2 text-center">
<h1>Some Text</h1>
</div>
#stop
My mainsection.blade.php
<section class="section">
<div class="container">
<div class="row">
#yield('section')
</div>
</div>
</section>
My default.blade.php
#include('layout.header')
<div id="wrapper">
<header>
<div class="container">
#include('partials.mainNav')
</div>
</header>
<div id="main">
#yield('content')
</div>
#include('layout.footer')
</div>
My thought was that i can take the partials.mainsection view for my content so that i dont have to write everytime the section / container / row code.
So i only write some text in #section('partials.mainsection') which is wrapped by the mainsection view.
But this does not work, what do i wrong or is there any other way to do this ?
The "partials.mainsection" is only my wrapper for my content(s) so i don't have to write this every time again. If i use just #include i can't extend it from each view.
To include a partial or any other partial into your blade template the syntax is #include('view.name')

Blade template not showing section

I have these three blade templates
In my controller I'm calling dashboard view like this:
View::make('layouts.dashboard');
master.blade.php
<?= stylesheet_link_tag() ?>
<?= javascript_include_tag() ?>
<html>
<body class="fixed">
<div class="wrapper">
<div class="leftside">
#yield('leftside', 'left-side content')
</div>
<div class="rightside">
#yield('rightside', 'right-side content')
</div>
</div>
</body>
dashboard.blade.php
#extends('layouts.master')
#section('leftside')
#yield('sidebar', 'sidebar content')
#stop
sidebar.blade.php
#extends('layouts.dashboard')
#section('sidebar')
aaa
#stop
The dasbhoard blade shows properly in master blade, but sidebar blade doesn't want to show. Does anyone know what i'm doing wrong?
Thank your very much for any help :)
I don't think you can use yield() on a blade template that is not called.
Basically what you can do is make your master.blade.php into this
<html>
<body class="fixed">
<div class="wrapper">
<div class="leftside">
#include('sidebar') {{-- This is sidebar.blade.php --}}
</div>
<div class="rightside">
#include('dashboard') {{-- This is dashboard.blade.php --}}
</div>
</div>
</body>
</html>
Then, assuming that your are calling the dashboard.blade.php template, with both blade templates included. You can use the sections on either master.blade, or sidebar.blade inside dashboard.blade
#extends('layouts.master')
{{-- You can define now the sections available on master.blade and dashbard.blade --}}
OK I found out what was wrong :)
I called View::make('layouts.dashboard') instead of View::make('layouts.sidebar');

Laravel Section not appear

I'll follow some tutorials about Laravel 3, and know, I have a problem, with one:
#section('post_navigation')
#if(Auth::check())
#include('plugins.loggedin_postnav')
#endif
#endsection
Why this section not appear?
I try remove all content in section, for example;
#section('post_navigation')
<h1>Test</h1>
#endsection
But doesn't work.
The complete code is that:
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
{{ HTML::link('/', 'Instapics', array('class' => 'brand')); }}
<div class="nav-collapse">
<ul class="nav">
#section('navigation')
<li class="active">{{ HTML::link('/', 'Home') }}</li>
#yield_section
</ul>
</div>
#section('post_navigation')
#if(Auth::check())
#include('plugins.loggedin_postnav')
#endif
#endsection
</div>
</div>
</div>
[EDIT]
I change #endsection to #yield_section, and works, BUT, I still not understand, for example (in User_Controller index view):
#section('post_navigation')
#parent
#endsection
Why not appear the include?
Why I need change endsection to yield_section?
When you use #section, you define a section like defining a variable. Then, you can use it where you want by #yield('sectionname') (or by using another way I specified in the second paragraph). For example, you can look at this:
#section('scripts')
<script src="jquery.js"></script>
#endsection
<head>
#yield('scripts')
</head>
#section / #endsection and #section / #yield_section are not same. #section / #yield_section defines a section area, not a section. In other words, it calls a variable. Actually it is more similar to yield('sectionname') than #section / #endsection. It has default value as a main difference from yield.
You can define an area which has default value, and then you can change it by defining a section. This logic mostly used while creating and using layouts. For example, Let below page be our main (main.blade.php) layout.
<html>
<head>
<title>
#section('title')
Default Title Maybe Sitename.com
#yield_section
</title>
#include('scripts')
</head>
<body>
<div id="content">
#section('content')
Default content
#yield_section
</div>
<div id="sidebar">
#section('sidebar')
<ul id="menu">
<li>Menu item 1</li>
<li>Menu item 2</li>
<li>Menu item 3</li>
<li>Menu item 4</li>
</ul>
#yield_section
</body>
</html>
Generally layouts are not used directly. Another page is created and specified in the page its layout like #layout('main'). Then, sections are defined and laravel templating system change the defined section. Let this page be our post (post.blade.php) page:
#layout('main')
#section('title')
$post->title - #parent
#end_section
#section('content')
$post->content
#end_section
When we return post view View::make('profile');, as you can see, layout logic will work and sections in main.blade.php will be changed with we defined in this page. So, the output will be:
<html>
<head>
<title>
Post title - Default Title Maybe Sitename.com
</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<div id="content">
Post content
</div>
<div id="sidebar">
<ul id="menu">
<li>Menu item 1</li>
<li>Menu item 2</li>
<li>Menu item 3</li>
<li>Menu item 4</li>
</ul>
</body>
</html>
By the way, #parent returns default value in section area and #include is same logic with include in php.
Have a nice day.
Seems like you may have found an answer. But it looks like #endsection has been replaced with #stop in L4. Try something like this instead:
#section('scripts')
<script src="jquery.js"></script>
#stop

Categories