#include sweetalert via laravel blade - php

I was reading sweet-alert README.md file when i saw this peace of code which is using laravel blade #include directive to call the swal function using the data stored in the session, and i was wondering how it works? what does the :: mean?
here is the code:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Include this in your blade layout -->
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
</head>
<body>
#include('sweet::alert')
</body>
</html>

It is for namespaced views, or views for packages.
Laravel 7.x Docs - Package Development - Views

Related

Blade template - #extends & #section not working

I'm learning the laravel framework and trying to get to grips with using the blade template engine. However i cant for life of me get the #extends and #section functionality to work within my project.
I have already tried reinstalling the whole project multiple times, using different browsers and restarting my machine but i cant figure out why it doesn't display the #section content
Laravel Version: 5.7.28 |
IDE: PhpStorm
routes/web.php
Route::get('/', function () {
return view('layouts/index');
});
views/layouts/index.blade.php
<body>
<div class="container-fluid">
<h1>Site Index</h1>
#yield('header')
</div>
</body>
views/header.blade.php
#extends('layouts.index')
#section('header')
<p>Header</p>
#endsection
At the moment all that is being displayed is the tag in the views/layouts/index.blade.php file.
Thank you very much for any and all input on this.
That's not how the templating works. You have to reference the child template in your return statement. Because the #extends is in this child template, Laravel knows to use the mentioned master layout. So your return statement would be like so:
return view('header');
If you just want the header to be displayed on every page, you don't need to extend the master layout in your header, you should just include the header part in your master layout.
<body>
<div class="container-fluid">
<h1>Site Index</h1>
#include('header')
</div>
</body>
i have tested the view and layout they seems working. check your controller return statement. try return view('header');
Route::get('/', function () {
return view('header');
});
thanks all for your responses, now i understand how the blade template engine works a little better and how i was doing this wrong. Just for clarification for others that get confused like me and come across this thread:
When you are redirecting to a view through the web routes then it has to be a child that is extending from a layouts master.
routes/web.php
Route::get('/', function () {
return view('index');
});
The html from the master file will then be displayed by default and its the content that we are "viewing"
views/layouts/master.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>#yield('title', 'default title if unspecified')</title>
</head>
<body>
<h1>Master Header</h1>
#yield('content')
</body>
</html>
To work with the content of the page then its the index view that is worked with using the #section('content') method.
views/index.blade.php
#extends('layouts.master')
#section('title', 'Changing the default title')
#section('content')
<p>content displayed</p>
#endsection
I hope this helps for anyone else.
If you want to show content of section('header') then you must return header view like
Route::get('/', function () {
return view('header');
});
this is because contents are in header view and you have been extending layout.index
so if you return layout.index view you will not see content of section('header')

Laravel - View [layouts.master] not found [duplicate]

This question already has answers here:
Laravel view not found exception
(19 answers)
Closed 2 years ago.
I'm making the Laravel tutorial step by step but the #extends is making me error that is View [layouts.master] not found. My layouts folder is in resources/views/layouts and my master.blade.php in the layouts folder. My connexion.blade.php is in resources/views/connexion.blade.php, so with the #extends('layouts.master') I'm not supposed to got any error. I'm maked the laravel tutorial perfectly but it seems to be strange.
RESOURCES/VIEWS/CONNEXION.BLADE.PHP
#extends('layouts.master')
#section('titre', 'Connexion')
#section('body')
#stop
RESOURCES/VIEWS/LAYOUTS/MASTER.BLADE.PHP
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Site Panel - #yield('titre')</title>
</head>
<body>
#yield('body')
</body>
</html>
The problem was not NOT FOUND but FILE PERMISSION why laravel dev didn't make PERMISSION DENIED instead of NOT FOUND
Hold up.... My layouts folder is in resources/views/layouts and my master.blade.php in the layouts folder.
So that means your master.blade.php is inside resources/views/layouts/layouts? That would mean the extend function would be #extend('layouts.layouts.master')
Blade Templates
Layout
// Extend your theme layout
#extends('layouts.master')
// Start section
#section('title')
// End Section
#endsection
#parent
// Show section in your theme
#yield('name')
// Include view in your file
#include('view.name')
// Include view with pass data
#include('view.name', ['key' => 'value']);

How to use AngularJS in .php file while rendering a laravel view

I am new to laravel and AngularJS . I am trying to render a view which is a php file. The .php file is being rendered but the AngularJS expression inside it is not being evaluated.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" >
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"> </script>
<script src='public/AngularSorter.js'></script>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"> </script>
</head>
<body ng-app = 'store'>
<div ng-controller ='SorterController as sorter'>
<p>
{{ 4+4 }}
</p>
</div>
</form>
</body>
</html>
the route is like this
Route::get('/', function () {
return view('Home');
});
Am I missing something? I tried renaming the php file to .html but it doesn't work. why can't the view render .html file??.
I get the output as {{4+4}} instead of 8.
Laravel Blade and AngularJS use the same syntax for processing variables, {}. To avoid this, you have to either change the syntax for blade or change the syntax for AngularJS. Details here.
Changing the AngularJS Syntax:
var sampleApp = angular.module('sampleApp', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
- or -
Changing the Laravel Blade Syntax:
// You may place this code anywhere it is executed each request. Some people have used routes.php
Blade::setContentTags('<%', '%>'); // for variables and all things Blade
Blade::setEscapedContentTags('<%%', '%%>'); // for escaped data
Also, the view will need to be .blade.php, not .html. This is the standard for all laravel blade (view) files. Documentation here: http://laravel.com/docs/master/blade
The issue was solved by reordering the script tags. the Angular script tag should be placed at the beginning and then the self written javascript files should be included.

Getting frustrated with Laravel 5 blades

For the past couple of days I have been trying to get into Laravel 5 blade system. Yet for some absurd reason I cannot get things to work. Here's how things look now:
UserController -> index
public function index()
{
return view('app');
}
So obviously we are getting a view so I create a blade file in views called app.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
</html>
Alright so we are all set so when I load the page all I get is "Document" in the title.
Here's the rub: if I change the title tag to something else like so:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Application</title>
</head>
<body>
</body>
</html>
And reload the page one would expect to see the title change? Well guess what? Nope. That didn't happen. The original title "Document" is still there.
Someone want to tell me whats going on here and how to fix it?
Are you sure it's not just cached? Try this: go to your \storage\framework\views and delete everything except .gitignore file, and try again.
If laravel is caching your view, clearing the browser cache won't help you as the caching is done server-side.
If you are actually using laravel 5.1, you can type on your terminal php artisan view:clear
More info: laravel.com/docs/5.0/cache

How to tell Laravel which script files to include

How can I tell Laravel-5 which Javascript files I want to include in a template.
For example, I have my Controller with something like this:
return view('butchery', ['locations' => $locations, 'classTypes' => $classTypes]);
I have my butchery.blade.php template with my html:
#extends('app')
#section('content')
<!-- html here -->
#endsection
...and I have my app.blade.php container template with all my javascript files at the bottom. If I didn't want to include one of the javascript files in my view, how would I do that?
app.blade.php:
<!DOCTYPE HTML>
<!--[if IE 8]><html class="ie8"> <![endif]-->
<!--[if gt IE 8]><!--><html><!--<![endif]-->
<head></head>
<body>#yield('content')</body>
<script src="/js/mobile-menu.js"></script>
<!-- apply voucher code -->
<script src="/js/voucher-apply.js"></script>
</html>
You should create a section in your template where the scripts should be loaded and use this in your pages to include the required scripts.
Like so:
Template:
<html>
<head>
<title>My page</title>
</head>
<body>
<div class="container">
#yield('content')
</div>
#yield('scripts')
</body>
</html>
Page:
#extends("template")
#section('content')
<h1>Hello world!</h1>
#stop
#section('scripts')
<script src="/path/to/script.js"></script>
#stop
This way you can include the scripts on the page that are needed. Scripts that are needed through the entire template can be added under or above the #yield('scripts').
You can use roumen/asset package. It is quite easy to use and will cover all your needs. Just set default set of assets somewhere (e.g. AppServiceProvider) and then in your controller add additional js files that you need only for this particular page. This is the easies way to manage your stack of assets.
Of course you can do that manually. But hen you have to make your view composer that will share array of js files and compile them in your view. Then again make somewhere array of default js/css files and in controller methods extend them.

Categories