While creating a basic item detail page for a basic blog I've run into a problem with Laravel's #extends not working when pulling in a master layout template. The content from the detail page loads just fine but the master page HTML is not there at all and I'm not getting any errors.
In my routes:
Route::get('blog', 'BlogController#public_index');
Route::get('blog/{id}', 'BlogController#public_single');
In my BlogController:
public function public_single($id) {
$blog = Blog::find($id);
return View::make('public.blog_single') -> with('blog', $blog);
}
At the very top of the blog_single template:
#extends('layouts.public')
All other templates that use this master layout work as expected.
My views directory structure:
views
|
|layouts
| |
| | admin.blade.php
| | public.blade.php
|
|public
|
|blog.blade.php
|blog_single.blade.php
One thing I'm wondering is if the fact that this page looks like it's rendering from a subdirectory is an issue. Here is an example:
This works:
www.mydomain.com/blog
This doesn't:
www.mydomain.com/blog/1
I've looked through the Laravel docs and don't see an answer there. Any ideas? Thanks.
Have you tried to add an absolute path to the master style references?
For example:
<link rel="stylesheet" href="css/bootstrap.min.css">
Has to be:
<link rel="stylesheet" href="/css/bootstrap.min.css">
In all the url's we use. This worked for me :)
Okay, not sure how this fixed it, hopefully someone can give me a little detail.
I found this line of code at the bottom of the blog_single.blade.php file:
<div class="text-center">{{ $blogs->links() }}</p>
That is leftover from a copy/paste of the blog.blade.php file and obviously on a detail page I don't need pagination.
I removed that one line of code and now the templates work as they should. Wondering why Laravel did not throw some sort of error if it was choking on that line, or why that line would effect the whole master file from getting included in the first place.
Anyway, it's fixed.
Related
I'm currently trying to set up a project in Fat-Free-Framework 3.6 - but I dislike the idea of having to manually add all my basic routes to routes.ini, and I decided I wanted to try my hand at more dynamic routes.
For this, I've made 2 new GET routes. Here is what I have:
$f3->route('GET /#module','{#module}_controller->index');
$f3->route('GET /#module/#method','{#module}_controller->#method');
The first one is to load a controller and it's default method (Which I have made index in these examples).
The first route works great, if I navigate my browser to http://example.com/ex1, it will load controller ex1_controller and run the index method, and everything will run and display properly.
The second route however, does not work as expected. When trying to run the exact same method within the second route, I get no CSS. For example, if I now navigate to http://example.com/ex1/index, there is no CSS on the page - but when I inspect the page source, I see the CSS should be loaded in as <link rel="stylesheet" type="text/css" href="assets/css/uikit.min.css"> is in the page.
Upon further investigation, I followed the link to the CSS file in my source code which brought me to the incorrect link. During the 2nd route, for some reason it's not trying to load from the root. It's trying to load from the URL ./ex1/assets/css/uikit.min.css, but the CSS actually exists in ./assets/css/uikit.min.css
Structure:
index.php - Where routes are defined
| app
| controllers
Controller.php
ex1_controller.php
| models
ex1.php
| views
| ex1
index.htm
| assets
| css
uikit.min.css
| js
uikit.min.js
ex1_controller.php
<?php
class ex1_controller extends Controller {
function index() {
$example = new ex1($this->db);
$output = $example->returnOutput();
$this->f3->set('output', $output);
echo $this->template->render('ex1/index.htm');
}
}
The Controller.php file is run on every route, which is then extended by the controller for the module. This is where the main_header.htm file is loaded in, which contains the css/js links. This is how the links are defined in main_header.htm;
<link rel="stylesheet" type="text/css" href="assets/css/uikit.min.css">
<script src="assets/js/uikit.min.js"></script>
<script src="assets/js/uikit-icons.min.js"></script>
Why is F3 trying to load CSS/JavaScript from the wrong directory when using dynamic routes such as this, and how can I fix it?
The fix for this seems to be quite easy.
Simply add a slash to the beginning of the linked resource path.
<link rel="stylesheet" type="text/css" href="/assets/css/uikit.min.css">
<script src="/assets/js/uikit.min.js"></script>
<script src="/assets/js/uikit-icons.min.js"></script>
Excuse me, i would like to ask about how to call another page blade in different subfolder inside views.
Example :
views
--home(subfolder)
--beranda(subfolder)
--refresh.blade.php
--layouts(subfolder)
--master.blade.php
in master.blade.php implements template page, when i click one link in this folder may have to go in refresh.blade.php.
Likely another web layout, they have a lot of link in header like 'Home', 'Paper', etc.
I'm still learned more about laravel as beginner practice.
May you can help me, i'll appreciate that.
Regard, Aga.
I think you can refer to some directives such as #include and #extends in the laravel blade.
For example, in the admin.common.header view (located at admin/common/header.blade.php), we have some basic page code (common to various pages such as navigation bar or layout). We use #yield such as #yield ("extra_js") or #yield ("extra_css") where we want to add code later.
header.blade.php
<html>
<head>
something maybe ...
#yield("extra_css")
</head>
<body>
something maybe ...
#yield("extra_js")
</body>
</html>
And in another view such as admin.feedback.feedback, you can use #extends('admin.common.header') at the top of the code to inheritance the template and you will get the layout of this template.
For different content in the feedback template, you can use #section to give you code to fill in the inheritance template such as #section('extra_js').
feedback.blade.php
#extends('admin.common.header')
#section('extra_js')
<script> something... </script>
#endsection
If you want to include one blade, just use #include.
<div>
#include('shared.errors')
<form>
<!-- Form Contents -->
</form>
</div>
In laravel blade there are many instructions to complete the rendering of the template, if you want to know clearly, please refer to the corresponding version of the official document.
I am working with Laravel and created a master.blade view file to use on all my pages.
The master view yields mini-views inside.
On most mini-views everything works fine, but on some, I don't get the background image from the master.
The problem is I still get the nav-bar and the footer on those pages, which means they do recognize the master view.
What can be the reason for that?
On all pages I use the exact same way to include and to yield:
#extends('master')
#section('content')
#endsection
Thanks alot!
Just use full path to your file, start with /
It's not really a big problem, but it is mighty annoying.
When starting creating templates in laravel using blade, everything looks fine. The "master" template/layout gets it's "styling" correct. And by "styling" i mean like correct amount of whitespaces, newlines etc when viewing the source code.
The problem occurs when you know try to extend this master template.
for every #section('<something>') you do know, all newlines is removed from the code, making the source code look fuggly.
Have been searching this phenomenon for a while without finding anything interesting which explains why or maybe a solution to make the source code readable again.
Here is an example if the explanation wasn't good enough:
// master.blade.php
<html>
<head>
<title>Something here</title>
</head>
<body>
#yield('content')
</body>
</html
Alright, this will look exactly like this in the source code. Let's make a another template which extends this.
// home.blade.php
#extends('master')
#section('content')
<h1>Welcome</h1>
<p>This is my homepage</p>
#endsection
This will first of inherit the parent, and replace #yield('content') with:
<h1>Welcome</h1> <p>This is my homepage</p>
Is there any explanation at all why this happens?
For longer sub-templates reading the source code is a living hell. Best way to see the "source code" is to see the generated one in inspect element, which also is just the live code, and not the first generated.
- Sligthly annoyed developer
I believe this is up to Laravels way of handling views. Every blade-view you create in resources/views will be "translated" to PHP. This line in your code for example:
#yield('content')
will be translated to
<?php echo $__env->yieldContent('content'); ?>
you can easely check for yourself by checking all files at storage/framework/views/. This are the files Laravel will include to build the "real" HTML website. Note, that all tab-stops made in .blade.php views are replaced with 4 spaces. And there are this ugly intendations.
I'm running into a weird issue where my page isnt' getting rendered in its entirety. The bottom bit is getting cut off and I'm not sure why.
I'd paste code, but it's a 3000+ line template im trying to implement.
I am doing it as follows in dashboard.master:
#include('dashboard.master.head')
...
#include('dashboard.master.header')
...
#include('dashboard.master.sidebar')
...
#include('dashboard.master.style_customizer')
...
#include('dashboard.master.page_header')
...
#yield('content')
...tons of lines here
It renders up to content correctly, and awhile after that but it seems to stop at some point.
example of my blade file calling my master
#extends('dashboard.master')
#section('content')
<p>This is my content</p>
#stop
Edit:
I tried removing everything and just pasting the HTML template I have. It still just stops rendering towards the end of the page. So all I have now is literally the master file and the includes in my blade template that calls the master.
Laravel will maintain a cache of views that have been accessed, just so that it doesn't have to compile the blade template for each page load. It should be refreshing a particular view should it change, but it sounds like, for some reason, the cache wasn't being refreshed.
You can clear the view cache in two different ways:
# Method 1
php artisan cache:clear
# Method 2
rm `ls /path/to/application/app/storage/views | grep -v '.gitignore'`
Reference
In order to show all page, change your last code to :
#extends('dashboard.master')
#section('content')
<p>This is my content</p>
#show