Load page specific resource(s) using #include - php

How do I load page specific resource(s) using #include of the Laravel blade template engine?
Below is the content of my Master layout (master.blade.php):
<head>
#section('styles')
{{-- Some Master Styles --}}
#show
</head>
<body>
{{-- Header --}}
#section('header')
#include('header')
#show
{{-- Content --}}
#section('content')
{{-- Content for page is extending this view --}}
#show
{{-- Footer --}}
#section('footer')
#include('footer')
#show
</body>
In a given page, I make use of my master template this way:
#extends('master')
#section('styles')
#parent
{{-- Page Stylesheet --}}
#endsection
The approach above is what I use to try and load my page specific style to the <head> section.
It does not work properly as expected.
I would like to as well load other page specific resource(s) in my footer using the same approach; how can I do that effectively?

You don't need to do
#extends('master')
#section('styles')
#parent
{{-- Page Stylesheet --}}
#endsection
in your respective pages so as to load page specific stylesheets.
You should rather load page specific stylesheets for your master.blade.php file so as to keep your code dry.
To to that, you shout specify the route or expected url format of such pages, then, the appropriate stylesheet(s) to be loaded.
You can do that this way in your master.blade.php file:
#section('styles')
#if(Request::is('transactions/generate-invoice'))
#include('generate-invoice-css')
#elseif(Request::is('transactions/users'))
#include('users-css')
#endif
#show
Where generate-invoice-css.blade.php contains the stylesheet(s) you want loaded for the page content accessible at yoursite.com/transactions/generate-invoice and users-css.blade.php, that of yoursite.com/transactions/users.
For a given pattern as in: same stylesheets for pages under transactions, you can do this:
#if(Request::is('transactions*'))
using a wildcard *.
To load a given resource to a location other than the <head> section of your pages, simply use the same approach and adapt as appropriate.
To load page specific resources with an #include() from your master.blade.php, use this approach (in your master.blade.php file):
#section('styles')
#include('styles')
#show
where styles.blade.php should contain those your conditions for the appropriate resources to be loaded satisfying your requirement(s) for the purpose as in:
#if(Request::is('transactions/generate-invoice'))
#include('generate-invoice-css')
#elseif(Request::is('transactions/users'))
#include('users-css')
#endif
As the content of your styles.blade.php.

Related

Laravel: extend a file in parent folder from subfolder

i want to extend the master.blade.php located in views\backend folder.
view/backend/master.blade.php <--i want to extend this file
view/backend/partials/header.blade.php <--the file that will extend the master.blade.php
folder structure
views
backend
-master.blade.php
partials
-header.blade.php
-footer.blade.php
-sidebar.blade.php
I tried this in the header.blade.php file but fail:
#extends('backend.master')
Edited
master.blade.php
<body>
<div class="wrapper">
#yield('header')
#yield('sidebar')
#yield('content')
#yield('footer')
</div>
</body>
header.blade.php
#extends('backend.master')
#section('header')
<p> this is the header</p>
#endsection
The page shows master content only in the browser
Normally header, footer and sidebar contain the markup which remain common across pages. Only the content varies from page to page.
Also the concept of extending master layout is to avoid repeating the shared parts across various pages. Using #yield('header') #yield('footer')
#yield('sidebar') defeats the concept of extending master layout. Because then all those sections need to be included on all pages.
So you master layout should be like
<body>
<div class="wrapper">
#include('backend.partials.header')
#include('backend.partials.sidebar')
#yield('content')
#include('backend.partials.footer')
</div>
</body>
Then for any page you can extend the master layout as
#extends('backend.master')
#section('content')
<!-- content markup here -->
#endsection

Alternative page title and meta data on login

I would like to know how to display alternative 'title' and other metadata on the login page.
Say I have a target URL say, https://example.com/thing/12345 ... if not logged in the user gets redirected to the login page, fine that's what I want, but on the login page I would like to inject the title, and maybe other metadata from the target page.
So that if a link such as above is shared in say Facebook, it will display the title/meta of the target URL and not the login URL.
AS per Laravel Documenation:
In your master template use #yield('title') to dynamically change the title
<html>
<head>
<title>App Name - #yield('title')</title> // add this code
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
From your blade template use #section('title') to pass title to master layout
#extends('layouts.master')
#section('title', 'Page Title') // pass dynamic title from here
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#stop
#section('content')
<p>This is my body content.</p>
#stop
Laravel -> Templates -> Blade Templating

Laravel blade append to section not working properly

I want to write to a region in my template from the view and also from an included view thing the first view but no matter what I try it doesn't work. I have tried using #parent and #append but nothing has produced the result that I want so far.
In my layout I have the following:
#yield('scripts')
In my view primary I have the following:
#include('admin/rules/partials/_form')
#section('scripts)
// javascript #1 here
#stop
In admin/rules/partials/_form I have the following:
#section('scripts)
// javascript #2 here
#stop
So like I said I have tried using #parent after #section('scripts') and also using #append instead of #stop but nothing I do includes the two script blocks properly. The best I've had so far is javascript #1 being included once and javascript #2 being included twice. How would I do this so that each block of code is appended to the scripts region only once?
I solved it in the end I had to do the following:
#yield('scripts')
In my view primary I have the following:
#include('admin/rules/partials/_form')
#section('scripts')
// javascript #1 here
#stop
In admin/rules/partials/_form I have the following:
#section('scripts')
#parent
// javascript #2 here
#overwrite
This answer worked for me.
EDIT, but now it doesn't.
EDIT, I updated the code below to work. I was yielding the javascript section on the page and the layout.
tests.layout.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>
#yield('title') - test
</title>
#yield('head')
</head>
<body>
#yield('content')
#yield('javascript')
</body>
</html>
tests.page.blade.php
#extends('layouts.default')
#section('title', 'Poses')
#section('content')
<div class="container">
<div class="row">
#include('tests.partials.one')
#include('tests.partials.two')
</div>
</div>
#stop
tests.partials.one.blade.php
#section('content')
#parent
<h1>One</h1>
#stop
#section('javascript')
#parent
<script>Scripts from one</script>
#stop
tests.partials.two.blade.php
#section('content')
#parent
<h1>Two</h1>
#stop
#section('javascript')
#parent
<script>Scripts from two</script>
#stop
For further reference: http://laravel.com/docs/5.0/templates
In my case, I'm not set the section #stop. After using #stop, it works properly.

How to modify layout sections from included views in laravel 4?

So,
I basically have a component which requires javascript to be loaded beforehand.
master layout:
//layouts/master.blade.php
...
#yield('scripts')
...
#include('forms.search')
...
my component:
//forms/search.blade.php
#section('scripts')
some scripts here
#stop
...
what Im calling:
//main.blade.php
#extends('layouts.master')
This does not work. Section is not added to header. Am I doing something wrong or it's not possible at all with laravel?
You are trying to yield the section before including. So try this.
In your //main.blade.php
#extends('layouts.master')
And in //layouts/master.blade.php
#include('forms.search')
And //forms/search.blade.php
some scripts here
you are calling
#extends('layouts.master')
which has a
#yield('scripts')
but you are declaring the section scripts on forms/search.blade.php
so if you examine correctly, you are declaring scripts on the wrong blade template OR you have put the yield area on the wrong blade template.. because since the #yield is on the layouts/master.blade.php, it had been executed already before the #include, which does not extend anything so declaring #section there wouldn't matter.
to achieve what you want, the
#section('scripts')
some scripts here
#stop
section should be in the main.blade.php file..
if i'm gonna do that, it would be something like this:
layouts/master.blade.php
<html>
<head>
<!-- more stuff here -->
#yield('scripts')
<!-- or put it in the footer if you like -->
</head>
<body>
#yield('search-form')
#yield('content')
</body>
</html>
forms/search.blade.php
//do whatever here
main.blade.php
#extends('layouts/master')
#section('scripts')
{{ HTML::script('assets/js/search-form.js') }}
#stop
#section('search-form')
#include('forms/search')
#stop
OR remove the #yield('search-form') totally on master.blade.php and on main.blade.php do this:
#section('scripts')
{{ HTML::script('assets/js/search-form.js') }}
#stop
#section('content')
#include('forms/search')
<!-- other stuff here -->
#stop
I was having the same problem, What got it working for me was adding "#parent" to ALL my sections...
{{-- Main area where you want to "yield" --}}
#section('js')
#show
#section('js')
#parent
{{-- Code Here --}}
#stop

Including external resources through a view that extends a layout

I'm trying to learn Laravel by using it for one of my sites however I'm having trouble working something out.
I have a layout.blade.php file which holds all my global markup. Stuff like the header, footer etc. I'm also including some external stuff like stylesheets and scripts in <head> like this:
{{ HTML::script('js/bootstrap.min.js'); }}
However, there's some external stuff that I only want to load on certain pages. I'm including individual views from the header like this:
<div id="content">
#yield('content')
</div>
and in the views I'm extending the layout like so:
#extends('layout')
#section('content')
<h1>some content</h1>
#stop
How can I use a HTML::script type function to include stuff in my <head> from a view that extends layout.blade.php?
Thanks!
In your layout.blade.php, wrap your HTML::script calls in a #section() and #show tags, and simply extend them afterwards, in your child view, just like you did with section, but also using #parent to include the parent's content. Example:
layout.blade.php
#section('javascript')
{{ HTML::script('js/jquery.js') }}
{{ HTML::script('js/bootstrap.min.js') }}
#show
childview.blade.php
#extends('layout')
// ...
#section('javascript')
#parent
{{ HTML::script('js/customScript.js') }}
#stop

Categories