Laravel 3 blade template is not working - laravel

I'm learning Laravel 3, but Blade template is not working. My code seems correct, but all it displays is #layout('master'). Pages source also contains this text only.
application\views\home\index.blade.php contents:
#layout('master')
#section('main')
{{ $greeting }}
#endsection
application\views\master.blade.php contents:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hello world!</title>
</head>
<body>
<div id="container">
#yield('main')
</div>
</body>
</html>
and in routes.php:
Route::get('/, home', function()
{
$greeting = "Hello world";
return View::make('home.index')->with('greeting',$greeting);
});
What can cause blade not to work? I tried Laravel 4 also, and changed #layout to #extends, #endsection to #stop but same situation. I get only #extends('master').

Not sure...but I think its because you have a space at the top of your file....
#layout('master')
needs to be directly at the top of the file for it to work properly...
Else it just echoes #layout('master') as words to the page, if you know what Im trying to say :)

same on me in L4...
check your charset of your code editor! sometimes some hidden chars are at the beginning of the file.
i switched to "UTF-8 without BOM"

Just remove every space or EOL, CR, LF, Enter, \n (or whatever) before your code.
Same issue in Laravel 4, I followed the quick start the step where you create a view I leave a new line.
-New line here, and it brokes-
#extends('layout')
#section('content')
Users!
#stop
Again remove anything before #extends('layout') and it works for me!!

Related

How do paths work in Vue.component() statement?

I am trying to learn Laravel and Vue properly, after an initial attempt back in the spring. I'm still pretty new to both though.
I am using Laravel 8.x in Windows 10 along with Vue 2.6.12. I am working my way through a video on combining Vue with Laravel. The video is for an older version of Laravel, probably 5.5, and probably a slightly older Vue as well so that may well be the nature of my problem. Here's the link to the video. I'm at about the 8:00 minute mark.
The problem I'm having is that when I try to execute my code, Laravel doesn't see my Articles component.
My app.js file is at /resources/assets/js/app.js. Here is the code:
require('./bootstrap');
window.Vue = required('vue');
Vue.component ('Articles', require('./components/Articles.vue'));
const app = new Vue({
el: '#app'
});
The file that contains the script tag is at /resources/views/welcome.blade.php. Here is the code:
<!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>Larticles App</title>
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
</head>
<body>
<div id="app">
<div class="container">
<Articles></Articles>
</div>
</div>
<script src="{{ asset('js/app.js') }}">console.log("Made it to here!");</script>
</body>
</html>
The Article component is at /resources/assets/js/components/Articles.vue. Here is the code:
<template>
<div>
<h2>Articles</h2>
</div>
</template>
<script>
export default {
name: "Articles",
beforeCreate() {
console.log("Articles - beforeCreate()");
},
created() {
console.log("Articles - created()");
},
beforeMount() {
console.log("Articles - beforeMount()");
},
mounted() {
console.log("Articles - mounted");
}
}
</script>
<style lang="scss" scoped>
#app {
background-color: gold;
color: blue;
}
</style>
What do I need to change to make this work? I think the issue is the require portion of the Vue.component statement in app.js but I've tried every variation I can think of without success. I can't think of anything else to try! I can't find anything in the Vue manual on special syntax for this statement in Vue or Laravel.
I should mention some things. I've deviated slightly from what he does in the video as part of my troubleshooting. I've capitalized Articles in the first parameter of the Vue.component statement and I've also capitalized it in the container div of the welcome.blade.php file. (Initially, I wrote it all lower case in each of those places but it didn't work that way either.) I've also added several console.log statements in the components lifecycle hooks and in the script tag. Absolutely none of them appear in the console though.
Also, for some reason, my IDE, VS Code, insists on displaying in red in the welcome.blade.php. Red always makes me think of errors but there is no error message of any kind. If I write those tags as (and change the app.js accordingly), they stay red so I don't think this is a casing issue.
I should also mention that the Inspector shows the as . Shouldn't it be showing exactly what I have in my welcome.blade.php file?
Use lower case letters when importing the component
<articles></articles>
Try to set each component you're reuire as default. Then use lower case as mentioned above. See below.
Vue.component ('Articles', require('./components/Articles.vue').default);
<articles></articles>

Laravel Blade #yield is not working inside included view

I am trying to make layout using blade but the problem is that when i tried to
#yield on the file which is included in master file but #yield is not working.
resouces/views/layouts/app.blade.php
<html>
<head>
...
...
</head>
<body>
#include('layouts.navigation')
#include('layouts.main_panel')
#include('layouts.footer')
</body>
</html>
resouces/views/layouts/main_panel.blade.php
// some html stuff
#yield('form')
// some html stuff
resouces/views/auth/login.blade.php
#extends('layouts.app')
#section('form')
<form>
// input
</form>
#endsection
I would suggest you pass variables to the partials and echo
them inside it. This is an alternative way to achieve what you are
trying to do.
For example -
Partial blade file (resouces/views/partials.header.blade.php) -
<h4>{{ $name }}</h4>
View (resouces/views/custom.blade.php) -
#include('partials.header', [ 'name' => 'Lorem Ipsum' ])
I am also using laravel framework but i used to do in that way :-
Layout :- resouces/views/layouts/app.blade.php
<html>
<head>
...
...
</head>
<body>
#include('layouts.navigation')
#yield('content') // use #yield here why you need separate file
#include('layouts.footer')
</body>
</html>
After that :- resouces/views/auth/login.blade.php
#extends('layouts.app')
#section('content')
<form>
// input
</form>
#stop
Hope it helps!.. I used to follow this structure in laravel project

How to use official Laravel documentation?

This may seem like an odd question, but I am new to Laravel and have a lot of trouble finding the right syntax in the official documentation. Here is an example. I was using the following blade syntax to try and have a conditional view:
#if (condition)
#extends 'layouts.regular'
#else
#extends 'layouts.special'
#endif
This did not work. I then went to the blade documentation on the Laravel site and read through the Extending a Layout section. All seemed well and I spent 10 minutes debugging my code and trying to find out if I did something wrong. It all seemed correct, so I then resorted to Google and found this thread.
From the comments I understood that:
The #extends statement must be the first line in a blade template
You can only use a conditional extend by using a ternary expression
While this solved my problem, it took too long to find. I still can't figure out WHERE in the official documentation this is mentioned. Am I using the official documentation wrong? Is there other, more extensive documentation out there? I keep feeling that the online documentation is very brief and often does not explain important details on syntax or exceptions...
#extends always comes in the first line of blade, it is used to define the layout e.g.
Assume we have master.blade.php
<html>
<head>
</head>
<body>
#yield('content')
</body>
</html>
Then we have page.blade.php
#extends('master') // this one says this page will be placed in master.blade.php
#section('content') // find the line that says #yield('content') and replace it with...
<h1> Hello World </h1>
#stop
Now, if you have many layouts and you want to share the content with it, you can achieve that programatically away from blade.. take this example
Assume we have master.blade.php
<html>
<head>
<title>Master</title<
</head>
<body>
#yield('content')
</body>
</html>
and we have master2.blade.php
<html>
<head>
<title>Master 2</title>
</head>
<body>
#yield('content')
</body>
</html>
and we have page.blade.php
#section('content')
<h1>Hello World</h1>
#stop
// note that we have no #extends()
now in the controller we do something like this
if($cond)
{
$layout = view('master');
}
else
{
$layout = view('master2');
}
$page = view('page');
$layout->content = $page;
return $layout;
Note that we are using $layout->content because both layouts are yielding content in #yield('content')

Blade is not working on laravel 4

This is what I have written in my view
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel PHP Framework</title>
</head>
<body>
<div class="welcome">
{{Hi everyone}}
</div>
</body>
</html>
It outputs {{Hi everyone}}
why is it not working ?
First: The view file should be named with a ".blade.php" after it.
Second: The {{ $str }} blade command it's only a shortcut to <?php echo $str ?>. That been said, if you want to just print a string, you should put it between " or even ', as you would probably do with a regular echo statement. Something like echo "Hi everyone";.
That SHOULD work on your case! :D
The file needs to be renamed as filename.blade.php. Check if the file had been named as per the convention rules.
You should call it in your route or controller with return View::make("hello"); etc. Also #Its Aafrin described, your file needs to be renamed as filename.blade.php.
There are diffrent ways to print. However you are just missing quotes. I have listed other methods also...
1. {{'Hi every one'}}
2. <?php $str='hi every one' ?> {{$str}}
3. <?= 'Hi every one' ?>

Blade Template Section Error

I am getting a syntax error using blade templating...
<title>
#section('title')
this is my title
#show
</title>
The generated output fails to close the opening PHP tag?
<title>
<?php $__env->startSection('title')
this is my title
<?php echo $__env->yieldSection(); ?>
</title>
If i do the following it works as expected...
<title>
#section('title'); ?>
this is my title
#show
</title>
Any ideas?
Thanks.
try changing your code as follows,
<title>
#section('title')
this is my title
#stop
</title>
Notice: I have changed #show to #stop inside your code.
It looks like an EOL conversion problem.
If you are using Notepad++ on Windows be sure that you have 'Edit/EOL Conversion/Windows Format'.

Resources