How do I load a CSS based on Laravel blade parameters? - laravel

I have this route:
Route::get('/{language}/{housingType}/{housingUrl}', 'HousingInfoController#index');
example: http://localhost/project/public/es_ES/H/this-is-housing-url
Sometimes it can have a parameter like this one:
http://localhost/project/public/es_ES/H/this-is-housing-url?group=ESQ
then on my controller I check if this parameter exists:
$group = '';
if ($request->input('group')) {
$group = $request->input('group');
}
and send it to the view:
return view('pages.HousingInfo.index', ['group' => $group]);
on my HousingInfo view I do:
#section('group', $group)
also my HousingInfo view extends from a layout, that layout includes a head, its a file with title, description, meta tags and also the CSS's.
There I have a:
#yield('group')
so I can know which parameter was sent on the URL.
My problem is I can't use yield for IF statement to show one CSS or another based on that parameter, How can I achieve that?
This give me error:
#if(#yield('group') == 'ESQ')
<link rel="stylesheet" href="{{ asset('css/ESQ.css') }}">
#else
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
#endif

Try this.
#if (!empty($group))
<link rel="stylesheet" href="{{ asset("css/$group.css") }}">
#else
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
#endif
This will enable you to use multi css if your group variable is not empty, if it is then use app.css

Related

How to pass data from #extends to #yield?

I have some common data getting from helper. Now I wanted to use that data in all over view. So, I am trying to declare that data in app.blade.php and trying to pass it's sections.
Here is my app.blade.php -
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>ABC| #yield('title')</title>
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<link href="{{ asset('/assets/plugins/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ asset('/assets/abc/css/style.css') }}" rel="stylesheet">
<?php
//get theme
$theme = AppHelper::instance()->getTheme();
//get theme folder
$themeFolder = $theme[0]->websiteAdmin;
//set include files section path
$includePath = 'frontend.'.$theme[0]->themeName.'.sections.';
?>
</head>
#yield('content')
</html>
Here I want to pass variables $theme, $themefolder, $includePath to #yield('content').
I tried below code -
#yield('content', array('theme'=> $theme, 'themeFolder'=> $themeFolder, 'includePath'=> $includePath))
But getting error that those variables are undefined.
Undefined variable: theme
Can you please help me how to pass data from #extend to #yield?
Thank you in advance.
You should use View composers to accomplish this. They allow you to pass the same data across multiple views by only calling it in one place.
In your providers/AppServiceProvider.php you can add the following to your boot method:
use Illuminate\Support\Facades\View; //import view facade
public function boot()
{
View::composer(['view-name', 'another-view-name'], function($view){
$theme = AppHelper::instance()->getTheme();
$themeFolder = $theme[0]->websiteAdmin;
$includePath = 'frontend.'.$theme[0]->themeName.'.sections.';
$view->with(compact('themeFolder', 'includePath', 'theme'));
});
}
View::composers first argument takes an array of views, put all the views you want to pass the data to here ['view-name', 'another-view-name'], it can also take a single string.
The data will now be available to your specified views through $themeFolder $includePath and $theme variables
If you want this data to pass to ALL views, you can do '*' as the first argument.
NOTE, this will pass the data to EVERY view you create, only do '*' if you want every view to contain the data! Otherwise, specifiy the views individually.
If you want all the views within a certain folder to get passed the data you can do 'folder-name.*'.
Alternatively, the only other way to pass data to your #yield is to return the view app.blade.php with the variables in your controller.

CSS getted away with view.blade.php

I have a .blade.php view that is nice and well designed with css, but when I put a variable like {{$data}} or even inside a for each loop {{$d->col1}} the css design get away.
What is the reason to be
#foreach($data as $d )
{{$d->price}}
#endforeach
I corrected myself
I changed
<link rel="stylesheet" href="css/bootstrap.min.css" rel="stylesheet">
to
<link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}" rel="stylesheet">
And the problem solved.

using the mix() helper in a bladeview inside a conditional giving errors

I just installed laravel 5.5 and the good news is, I finally managed to get npm run dev/production working. The manifest file looks fine too. In my bladeview I am using these lines:
#if($userIsLoggedIn)
<link href="{{ mix('build/ccs/admin.css') }}" rel="stylesheet">
#else
<link href="{{ mix('build/ccs/auth.css') }}" rel="stylesheet">
#endif
But loading the page results in the following error:
So I was like, lets go into the mix helper and dump some data to see what it looks like... :
dd($manifest, $path); gives :
Is it just me, or does the index actualy exists? using
dd($manifest[$path]);
actualy results in Undefined index. The weirdest thing, when I write the link outside of the conditional like this:
<link href="{{ mix('build/css/auth.css') }}" rel="stylesheet">
<!-- Styles -->
{{-- #if($userIsLoggedIn)
<link href="{{ mix('build/ccs/admin.css') }}" rel="stylesheet">
#else
<link href="{{ mix('build/ccs/auth.css') }}" rel="stylesheet">
#endif --}}
then if all works fine! no errors or undefined indexes whatsoever, versioning/non versioning.. all loads :P There is some stuff going on here that I don't understand.. Any1 has any ideas??
EDIT::
Using these lines at the bottom of the body works fine too..
#if($userIsLoggedIn)
<script src="{{ mix('/build/js/admin.js') }}"></script>
#endif
)) try to change way of css files)
mix('build/ccs/admin.css') => mix('build/css/admin.css')
etc.)
It should be 'build/css/admin.css' instead of build/ccs/admin.css.

I lose my bootstrap styles when passing variable (get) to route->Controller->View in Laravel 5.4

I lose my bootstrap styles when passing variable (get) to route->Controller->View in Laravel 5.4.
here is my code-
header.blade.php->
<a href="{{URL::to( '/news/'.$v_all_published_category->id )}}">
web.php->
Route::get('/news/{id}', 'NewsController#view_news');
NewsController.php->
public function view_news($id) {
$category_info = Category::where('publication_status', 1)->get();
$category_product = Product::where('id', $id)->get();
$data = array();
$data['header_content'] = view('Front_End.includes.header', ['all_published_category' => $category_info]);
$data['home_slider']= view('Front_End.slider.home_slider');
$data['main_content'] = view('Front_End.category.category_content', ['category_product' => $category_product]);
$data['footer_content']= view('Front_End.includes.footer');
return view('Front_End.master')->with($data);
}
#EdwardPalen
<!DOCTYPE html>
<html>
<head>
<title>NewsFeed</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="{{ asset('page_assets/assets/css/bootstrap.min.css')}}">
<link rel="stylesheet" type="text/css" href="{{ asset('page_assets/assets/css/font-awesome.min.css')}}">
<link rel="stylesheet" type="text/css" href="{{ asset('page_assets/assets/css/animate.css')}}">
<link rel="stylesheet" type="text/css" href="{{ asset('page_assets/assets/css/font.css')}}">
<link rel="stylesheet" type="text/css" href="{{ asset('page_assets/assets/css/li-scroller.css')}}">
<link rel="stylesheet" type="text/css" href="{{ asset('page_assets/assets/css/slick.css')}}">
<link rel="stylesheet" type="text/css" href="{{ asset('page_assets/assets/css/jquery.fancybox.css')}}">
<link rel="stylesheet" type="text/css" href="{{ asset('page_assets/assets/css/theme.css')}}">
<link rel="stylesheet" type="text/css" href="{{ asset('page_assets/assets/css/style.css')}}">
</head>
<body>
<a class="scrollToTop" href="#"><i class="fa fa-angle-up"></i></a>
<div class="container">
#yield('header_content')
#yield('slider')
#yield('main_content')
#yield('footer_content')
</div>
{{ Html::script('page_assets/assets/js/jquery.min.js') }}
{{ Html::script('page_assets/assets/js/wow.min.js') }}
{{ Html::script('page_assets/assets/js/bootstrap.min.js') }}
{{ Html::script('page_assets/assets/js/bootstrap.min.jss') }}
{{ Html::script('page_assets/assets/js/jquery.li-scroller.1.0.js') }}
{{ Html::script('page_assets/assets/js/jquery.newsTicker.min.js') }}
{{ Html::script('page_assets/assets/js/jquery.fancybox.pack.js') }}
{{ Html::script('page_asset`enter code here`s/assets/js/custom.js') }}
</body>
</html>
Have you tried using the blade helper tags?
When the directory level in the URL changes the styles don't get found. You should use the helpers Laravel provides to generate a full URL.
Try to integrate it like this in the app.blade.php file:
There's assets()
<link href="{{ asset('assets/css/bootstrap.min.css') }}" rel="stylesheet">
Or even a helper to create the full link tag for including:
{{ HTML::style('assets/css/bootstrap.min.css') }}
(And there's one for javascript files too)
{{ HTML::script('assets/js/script.js') }}
You can take as reference of this issue in this link:
I lose my bootstrap styles when passing variable (get) to route->Controller->View in Laravel 4
A solution and method that I use frequently.
<link href="{{ asset('assets/css/bootstrap.min.css') }}" rel="stylesheet">
You can include CSS and JS files, and you can put them directly in the public / css or public / js folder and then link them from the layout with the asset () function:

Problems with #yield and templating

I'm very new to Laravel and I'm trying to figure out how to use the templating.
I feel so sure that what I have here should work, but I keep getting an error when I run it. I think it has to do with having to #yield commands on the same line. Is this simply a limitation of the blade engine?
routes.php
Route::get('/', function()
{
return View::make('hello');
});
// Test Route:
Route::get('jtest', function(){
$page = array(
"lang" => "en",
"title" => "jtest",
"css" => "css/layout.css",
"rand" => rand()
);
return View::make('jtest')->with('page', $page);
});
jtest.blade.php
#extends('layout')
#section('html-lang')
#if ( isset($page['lang']) )
{{ $page['lang'] }}
#endif
#endsection
#section('title')
#if ( isset($page['title']) )
{{ $page['title'] }}
#endif
#endsection
#section('meta-description')
#if ( isset($page['meta-description']) )
{{ $page['meta-description'] }}
#endif
#endsection
#section('css')
#if ( isset( $page['css'] ) )
{{ $page['css'] }}
#endif
#endsection
#section('rand')
#if ( isset( $page['rand'] ) )
{{ $page['rand'] }}
#endif
#endsection
layout.blade.php
<!doctype html>
<html lang="#yield('html-lang')">
<head>
<meta charset="utf-8">
<title>#yield('title')</title>
<meta name="description" content="#yield('meta-description')">
<meta name="author" content="Jimmy Hogoboom">
<link rel="stylesheet" href="#yield('css')?r=#yield('rand')">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<script src=""></script>
</body>
</html>
And the error is
syntax error, unexpected '='
and the line the error's on looks like this:
<link rel="stylesheet" href="<?php echo $__env->yieldContent('css')?r=#yield('rand'); ?>">
So the problem is here:
<link rel="stylesheet" href="#yield('css')?r=#yield('rand')">
If I remove the second #yield:
<link rel="stylesheet" href="#yield('css')?r=">
the page loads fine.
So is this just a limitation of blade, or is there some other way I should be placing these values in the page?
The best idea is to use #yield and sections only for bigger parts like main content or for a sidebar.
If you need 'isset' condition you can use
#if(isset($title)){{$title}}#endif
But this also should be done on controller with clean php including default value for a variable.
I am not sure why you need to use #yield for a small thing.
the following simple code will do the job:
<link rel="stylesheet" href="{{ $page['css'] }}?r={{ $page['rand'] }}">

Resources