Interpolate #yield() value within {{}} or the asset() helper in Laravel - php

I want to interpolate a #yield('page-name') value in the asset() helper method:
I think that blade directives cannot be used with php (I mean within {{}})
This is what I'm trying to do:
//guest.blade.php file
<html>
<head>
<title>{{ config('app.name', 'Laravel') }} - #yield('title')</title>
// this throws an error
// ParseError: syntax error, unexpected token "<"
<link rel="stylesheet" href="{{ asset('css/'. #yield('view-asset') .'.css') }}">
</head>
<body>
#yield('content')
</body>
</html>
//login.blade.php file
#extends('layouts.guest')
#section('title', 'Login-page')
#section('view-asset')
#section('content')
...
#endsection
How can do a similar implementation, to what I'm trying to do here?

You can use view() helper function.
Illuminate\View\View namespace holds Illuminate\View\Factory in it which contains data like files, paths, hints, views, section, etc.
To get yield data (retrieved from section directive of child view), In your parent view:
view('your.view')->getFactory()->getSection('page-name');
You can also pass a default value for the section.
view('your.view')->getFactory()->getSection('page-name' , 'default value');
Which in your case it would be like this:
<link rel="stylesheet" href="{{ asset('css/'.view("directory.guest")->getFactory()->getSection("page-name" , "default_value").'.css') }}">

Related

Append stylesheet from child component laravel

I have a master blade template and a component.
I tried to include my stylesheet only for my component, not the whole template how can I do that?
My attempt was using #stack as described here
view/layouts/master.blade.php
<head>
...
...
#stack('styles')
</head>
view/books/index.blade.php
...
...
#push('styles')
<link href="{{ asset(css/styles.css) }}" rel="stylesheet">
#endpush
...
...
Solved, my css file was on resources folder, it should be on a public folder

Use of undefined constant 'lte' error in Laravel

I'm new in Laravel and I trying to use asset helper in Laravel, but returned error:
undefined constant
Here's my code
<link rel="stylesheet" href="{{ asset(lte/plugins/fontawesome-free/css/all.min.css) }}">
PS : lte is a directory which the css and js are placed
Can you tell me what's wrong in my code?
asset() helper need path of assets as string so it will be
<link rel="stylesheet" href="{{ asset('lte/plugins/fontawesome-free/css/all.min.css') }}">
asset('path') here ' is important
for more info you can check official doc https://laravel.com/docs/8.x/helpers#method-asset

Laravel's Blade template #yield and #section how does it work?

I'm new to Laravel and want to learn how to use the Blade template system properly, but i cant wrap my head around the difference between #section and #yield.
I've been reading the docs : https://laravel.com/docs/5.7/blade.
But it's not explaining the differences and how to use them properly.
I've been reading posts in other forums too like this one :
https://laravel.io/forum/09-02-2014-using-section-and-yield
But still i'm a bit confused.
For example right now i'm creating an app that have multiple pages with commun pieces between them, so for now i get that i have to create a common layout for this pages, but when to use #section and when do i have to use #yield ?
for example if i have a page like so :
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<title>{{ config('app.name', 'Name') }}</title>
//Common CSS between pages
<link href="{{ asset('css/style1.css') }}" rel="stylesheet">
//Changing CSS between pages
<link href="{{ asset('css/style2.css') }}" rel="stylesheet">
</head>
<body>
//the content stay the same !
<div id="app">
<span id="some_style">hello world !</span>
</div>
<script>
//common JS
<script src="{{ asset('script1.js') }}">
//Changing JS between pages
<script src="{{ asset('script2.js') }}">
</script>
</body>
</html>
How can i organise it using the blade templating?
Assuming you 2 templates. Lets call one Base.blade.php and the other one Posts.blade.php.
We'll #extends('base') in Posts.
Using #section in Posts and #yield in Base.
Something like this:
Base
#yield('posts') {# the section called "posts" #}
Posts
#extends('base')
#section('posts')
Here be posts
#endsection
Whatever is written in posts will be yielded in the base blade.
Think of it as inheritance.
You can imagine it as classes if you will. Where the child class calls a method in the base class.
class Base {
protected function printSomething($something) {
echo $something;
}
}
class Posts extends Base {
public function BaseWillPrint() {
$this->printSomething('POSTS');
}
}
Basically I haven't told you anything that doesn't already exist in the documentation.
Yes, assuming if you have a template you can make the base of template in layouts/app.blade/php and you can make the #yield('anything') and then in your views/main.blade.php you must give #extends('layouts.app') and
#section('anything')
*for dynamic page
#endsection

Tag helpers on external URLs without protocol

I am attempting to use volt tag helpers such as stylesheet_link to link to an external URL without protocol prefix.
Example usage without protocol proves to be problem free:
{{ stylesheet_link('http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css') }}
Generates
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
.
However, whenever the protocol prefix is obmitted (ie/ //netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css):
{{ stylesheet_link('//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css') }}
Generates incorrect URL:
<link rel="stylesheet" type="text/css" href="[SITE_PREFIX]//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
Is it due to my incorrect usage? or is this an existing bug with volt?
Try specifying that it's not local, see the docs.
{{ stylesheet_link('//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css', false) }}

Using CSS in Laravel views?

I've just began learning Laravel, and can do the basics of a controller and routing.
My OS is Mac OS X Lion, and it's on a MAMP server.
My code from routes.php:
Route::get('/', function() {
return View::make('home.index');
});
Route::get('businesses', function() {
return View::make('businesses.index');
});
Route::get('testing', function() {
return View::make('testing.index');
});
Route::get('hello', function() {
return "<h3>Hello world!</H3>";
});
That works, the views display perfectly, ''however'' what I want to try and do is include CSS within the views, I tried adding in a link to a stylesheet within the directory but the page displayed it as the default browser font even though the css was in the HTML!
This is index.php from businesses within the views folder:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<p>Business is a varied term. My content here.
I tried using the Blade template engine in my other views folder (testing) to display CSS but again the CSS did not show despite it being in the testing folder!
How can I overcome this problem, and get better - as I'm slowly learning this framework.
Put your assets in the public folder; e.g.:
public/css
public/images
public/fonts
public/js
And then, to access them using Laravel, use:
{{ HTML::script('js/scrollTo.js'); }}
{{ HTML::style('css/css.css'); }}
Or:
{{ URL::asset('js/scrollTo.js'); }}
{{ URL::asset('css/css.css'); }}
This syntax will automatically generate the correct path (e.g., `public/js/scrollTo.js').
your css file belongs into the public folder or a subfolder of it.
f.e if you put your css in
public/css/common.css
you would use
HTML::style('css/common.css');
In your blade view...
Or you could also use the Asset class http://laravel.com/docs/views/assets...
You can also write a simple link tag as you normaly would and then on the href attr use:
<link rel="stylesheet" href="<?php echo asset('css/common.css')?>" type="text/css">
of course you need to put your css file under public/css
We can do this by the following way.
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
{{ HTML::style('css/style.css', array('media' => 'print')) }}
It will search the style file in the public folder of Laravel and then will render it.
Like Ahmad Sharif mentioned, you can link stylesheet over http
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
but if you are using https then the request will be blocked and a mixed content error will come, to use it over https use secure_asset like
<link href="{{ secure_asset('/css/style.css') }}" rel="stylesheet">
https://laravel.com/docs/5.1/helpers#method-secure-asset
Since Laravel 5 the HTML class is not included by default anymore.
If you're using Form or HTML helpers, you will see an error stating class 'Form' not found or class 'Html' not found. The Form and HTML helpers have been deprecated in Laravel 5.0; however, there are community-driven replacements such as those maintained by the Laravel Collective.
You can make use of the following line to include your CSS or JS files:
<link href="{{ URL::asset('css/base.css') }}" rel="stylesheet">
<link href="{{ URL::asset('js/project.js') }}" rel="script">
You can simply put all the files in its specified folder in public like
public/css
public/js
public/images
Then just call the files as in normal html like
<link href="css/file.css" rel="stylesheet" type="text/css">
It works just fine in any version of Laravel
Update for laravel 5.4 ----
All answers have have used the HTML class for laravel but I guess it has been depreciated now in laravel 5.4, so
Put your css and js files in public->css/js
And reference them in your html using
<link href="css/css.common.css" rel="stylesheet" >
To my opinion the best option to route to css & js use the following code:
<link rel="stylesheet" type="text/css" href="{{ URL::to('route/to/css') }}">
So if you have css file called main.css inside of css folder in public folder it should be the following:
<link rel="stylesheet" type="text/css" href="{{ URL::to('css/main.css') }}">
That is not possible bro, Laravel assumes everything is in public folder.
So my suggestion is:
Go to the public folder.
Create a folder, preferably named css.
Create the folder for the respective views to make things organized.
Put the respective css inside of those folders, that would be easier for you.
Or
If you really insist to put css inside of views folder, you could try creating Symlink (you're mac so it's ok, but for windows, this will only work for Vista, Server 2008 or greater) from your css folder directory to the public folder and you can use {{HTML::style('your_view_folder/myStyle.css')}} inside of your view files, here's a code for your convenience, in the code you posted, put these before the return View::make():
$css_file = 'myStyle.css';
$folder_name = 'your_view_folder';
$view_path = app_path() . '/views/' . $folder_name . '/' . $css_file;
$public_css_path = public_path() . '/' . $folder_name;
if(file_exists($public_css_path)) exec('rm -rf ' . $public_css_path);
exec('mkdir ' . $public_css_path);
exec('ln -s ' . $view_path .' ' . $public_css_path . '/' . $css_file);
If you really want to try doing your idea, try this:
<link rel="stylesheet" href="<?php echo app_path() . 'views/your_view_folder/myStyle.css'?>" type="text/css">
But it won't work even if the file directory is correct because Laravel won't do that for security purposes, Laravel loves you that much.
put your css File in public folder . (public/css/bootstrap-responsive.css)
and <link href="./css/bootstrap-responsive.css" rel="stylesheet">
Copy the css or js file that you want to include, to public folder or you may want to store those in public/css or public/js folders.
Eg. you are using style.css file from css folder in public directory then you can use
<link rel="stylesheet" href="{{ url() }}./css/style.css" type="text/css"/>
But in Laravel 5.2 you will have to use
<link rel="stylesheet" href="{{ url('/') }}./css/style.css" type="text/css"/>
and if you want to include javascript files from public/js folder, it's fairly simple too
<script src="{{ url() }}./js/jquery.min.js"></script>
and in Laravel 5.2 you wll have to use
<script src="{{ url('/') }}./js/jquery.min.js"></script>
the function url() is a laravel helper function which will generate fully qualified URL to given path.
Put them in public folder and call it in the view with something like href="{{ asset('public/css/style.css') }}". Note that the public should be included when you call the assets.
put your css in public folder, then
add this in you blade file
<link rel="stylesheet" type="text/css" href="{{ asset('mystyle.css') }}">
Use {!! in new laravel
{!! asset('js/app.min.js') !!}
<script type="text/javascript" src="{!! asset('js/app.min.js') !!}"></script>
For Laravel 5.4 and if you are using mix helper, use
<link href="{{ mix('/css/app.css') }}" rel="stylesheet">
<script src="{{ mix('/js/app.js') }}"></script>
For those who need to keep js/css out of public folder for whatever reasons, in modern Laravel you can use sub-views. Say your views structure is
views
view1.blade.php
view1-css.blade.php
view1-js1.blade.php
view1-js2.blade.php
in view1 add
#include('view1-css')
#include('view1-js1')
#include('view1-js2')
in views-js.blade.php files wrap your js code in <script> tag
in views-css.blade.php wrap your styles in <style> tag
That will tell Laravel, and your code editor, that those are in fact js and css files. You can do the same with additional HTML, SVGs and other stuff that is browser-renderable

Categories