Laravel Section not appear - php

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

Related

Laravel #yield is undefined for HTML section

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

Laravel 8 with breadcrumbs add dynamic page title

I'm using laravel-breadcrumbs package by diglactic with laravel 8 forked from davejamesmiller/laravel-breadcrumbs I'm trying to add dynamic page title based on this breadcrumbs package.
what I mean by dynamic page title ? let's imagine that every page I have it contain a page title section like this :
HTML: (Every Page it have this section below the navbar)
<section class="py-5 text-center container">
<div class="row py-lg-5">
<div class="col-lg-6 col-md-8 mx-auto">
<h1 class="fw-bold mb-4">Page Title Here</h1> // Dynamic Page Title Here
</div>
</div>
</section>
You can make your own breadcrumb. First you should generate blade component something like php artisan make:component breadcrumb and then look at the code, I use it anyway and it success
AppServiceProvider.php
public function boot()
{
// your prev code
view()->share('currentPath', explode('/', substr_replace(request()->path(), '', 0, 3)));
}
componenents/breadcrumb.blade.php
<nav>
<ul class="breadcrumbs">
<li class="breadcrumb-item pl-0">
Home
</li>
#foreach ($paths as $path)
<li class="breadcrumb-item pl-0 text-capitalize
{{ $loop->last ? 'active' : '' }}">
<a href="{{ url($path) }}">
{{ str_replace('-', ' ', $path) }}
</a>
</li>
#endforeach
</ul>
</nav>
And use it in any view like this
<x-breadcrumb :paths="$currentPath" />
You can use #yield to cahnge a section of your template dynamically.
check the documentation for more info.
The #yield is use to dynamic title
here i am create a template.blade.php file and title.blade.php.
the title.blade.php is extends template.blade.php file and title can dynamic is work perfectly.
template.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>#yield('title')</title>
</head>
<body>
The title is dynamically
</body>
</html>
title.blade.php
#extends('template')
#section('title')
My Title
#endsection

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>

Sending data to a view extended by other views in Laravel

I have a main view that is extended by different other views. Main views has a header and left sidebar.
Headers has 3 buttons with bubbles on it: Stories, New User and, Returning Users. Bubble on menu display information, like bubble on New Users shows count of visitors who signed up today.
This view is extended by all other views used in different controller methods.
This is the main view
<html>
<head>
</head>
<body>
{{-- Header--}}
<ul id="headernav">
<li>
<ul>
<li>Hoots & Stories<span>{{$todayHootStories}}</span></li>
<li>New Users<span>{{$newVisitorsToday}}</span></li>
<li>Returning Users<span>{{$retVisitorsToday}}</span></li>
</ul>
</li>
</ul>
{{-- Sidebar --}}
<nav>
<ul id="nav">
<li class="i_house">
<a href="{{route('dashboard')}}">
<span> Overview</span>
</a>
</li>
<li class="i_user">
<a href="{{route('users')}}">
<span>Users</span>
</a>
</li>
<li class="i_user">
<a href="{{route('categories')}}">
<span>Categories</span>
</a>
</li>
</ul>
</nav>
{{-- Space For Content --}}
<div id="content">
#yield('content')
</div>
</body>
</html>
Header needs three variables $todayHootStories, $newVisitorsToday, $retVisitorsToday. Now i need to include these variables from each method of different controllers to make it work properly.
Is there any other way around?
I think you are looking for View composers
View::composer('profile', function($view)
{
$view->with('count', User::count());
});
You can find a good tutorial on adding this to your layout here.

laravel 4 - how to show and hide components in blade?

I have breadcrumbs in my master.blade.php file and i want the breadcrumbs to be used everywhere BUT the homepage.blade.php.
right now this is how i add links to the breadcrumbs in other pages like "About".
about.blade.php:
#section('breadcrumbs')
#parent
<li class="last-breadcrumb">
About
</li>
#stop
in the master.blade.php:
<div class="container">
<div class="breadcrumb-container">
<ul class="breadcrumb">
#section('breadcrumbs')
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
#show
</ul>
</div>
</div>
but i don't want the breadcrumbs code to display at all when homepage.blade been used.
copying the code for each about.blade/X.blade files seems like a bad idea..
Your can set a value in your controller that you pass with the make/redirect like $data['breadcrumb'] = true and wrap your breadcrumb code in an conditional. This kind of system also works well for error and success messages since you can alse send content from your controller. Then you would send an array with values instead of true.
Blade template
<div class="container">
#if($breadcrumb)
<div class="breadcrumb-container">
<ul class="breadcrumb">
#section('breadcrumbs')
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
#show
</ul>
</div>
#endif
</div>
You can check the URI to see if you want to display it or not. It's logic in your view which is usually frowned upon, but it's the easiest way I can come up with.
<div class="container">
<div class="breadcrumb-container">
<ul class="breadcrumb">
#if(Route::uri() == '/')
<li class="last-breadcrumb">
About
</li>
#endif
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
#show
</ul>
</div>
</div>
Depending on the URI you are using for your home page, you may need to tweak the condition.
If you use #yield in your master template then it will only display content when the section has been defined. #content requires the section to be always defined.

Categories