How to EXTEND a blade template only if it exists? - php

Basically I want to #extend a blade template only if it exists, and if not #extend a different template. There are a few stack overflow answers concerning using #if #endif blocks, but that only works if you are #including a file, not #extending. Ideally something like this, but it doesn't work:
#if(some_condition == true)
#extends('one')
#else
#extends('two')
#endif
If the only way is to use Blade directives, could you please provide an example? Thank you!

Try doing it like this:
#extends( $somecondition == true ? 'one' : 'two')

you can use view:exists
#if(View::exists('path.to.view.one'))
#extends('one')
#else
#extends('two')
#endif

You can use View::exists
#if (View::exists('one'))
#extends('one')
#else
#extends('two')
#endif
or
file_exists() and to get the path use resource_path() this,
#if (file_exists(resource_path('views/one.blade.php')))
#extends('one')
#else
#extends('two')
#endif
You can try this, in my case that's working.. See the docs in Laravel - https://laravel.com/docs/5.5/views

#if( file_exists('path to file one'))
#extends('one')
#else
#extends('two')
#endif

you can use the conditional to define the name of the view you want to load and then simply extend it, for example:
#php
$view = '';
if (some_condition == true) {
$view = 'one';
} else {
$view = 'two';
}
#endphp
...
#extends($view)
more info
https://laravel.com/docs/5.5/blade#php

Related

Using Route::is() to check if the route is the homepage

How can i check to see if the current route is the root page?
i've tried in a blade file typing thie following:
#if(Route::is('/')
//show something
#else
//show something else
#endif
i've also tried it with Route::currentRouteName() such as
#if(Route::currentRouteName() == '/')
//do something
#else
//do something else
#endif
and it doesn't seem to work on the root page.
is there an alternative way? or is it that the root doesn't have a route?
#if(Request::is('/'))
//do something
#else
//do something
#endif
Route::get('/', 'HomeController#index')->name('root');
#if(Route::currentRouteName() == 'root')
//do something
#else
//do something else
#endif
you can use your function to check currentRouteName, you just need to make sure you set the name for that route like I mentioned below:
Route::get('/', 'HomeController#index')->name('home');
#if(Route::currentRouteName() == 'home')
//do something
#else
//do something else
#endif
Alternatively, you can also use routeIs():
#if(request()->routeIs('home'))
// yay
#else
// nay
#endif
The helper works like the normal "is", which means that it supports multiple patterns, but matches against route names instead.

How can I check auth user role in laravel?

I have role column in users table, and I want to check the value like this in the blade file :
#if ( {{Auth::user()->role }} == '1')
// do something
#endif
Is it possible ?
In blade files, you need to write plain PHP into the #if and others blade statements. So you would need to remove the {{ }}:
#if ( auth()->user()->role == 1)
// do something
#endif
I think you can extend a blade.
https://laravel.com/docs/5.3/blade#extending-blade
It's cool and convenient.
Latest version of Laravel will work with like this. You don't need to use {{}} here.
#if ( Auth::user()->role == 1)
// do something
#endif
#if(\Illuminate\Support\Facades\Auth::user()->hasRole('Admin') == 'Admin')
// do something
#endif

Laravel 4.2 blade: check if empty

In Laravel blade you can do:
{{ $variable or 'default' }}
This will check if a variable is set or not. I get some data from the database, and those variables are always set, so I can not use this method.
I am searching for a shorthand 'blade' function for doing this:
{{ ($variable != '' ? $variable : '') }}
It is hard to use this piece or code for doing this beacuse of, I do not know how to do it with a link or something like this:
{{ $school->website }}
I tried:
{{ ($school->website != '' ? '{{ $school->website }}' : '') }}
But, it does not work. And, I would like to keep my code as short as possible ;)
Can someone explain it to me?
UPDATE
I do not use a foreach because of, I get a single object (one school) from the database. I passed it from my controller to my view with:
$school = School::find($id);
return View::make('school.show')->with('school', $school);
So, I do not want to make an #if($value != ''){} around each $variable (like $school->name).
try this:
#if ($value !== '')
{{ HTML::link($value,'some text') }}
#endif
I prefer the #unless directive for readability in this circumstance.
#unless ( empty($school->website) )
{{ $school->website }}
#endunless
With php 7, you can use null coalescing operator. This is a shorthand for #m0z4rt's answer.
{{ $variable ?? 'default' }}
{{ ($school->website != '' ? '{{ $school->website }}' : '') }}
change to
{{ ($school->website != '') ? '' . $school->website . '' : '' }}
or the same code
{{ ($school->website != '') ? "<a href='$school->website' target='_blank'>$school->website</a>" : '' }}
{{ isset($variable) ? $variable : 'default' }}
I wonder why nobody talked about $variable->isEmpty() it looks more better than other. Can be used like:
#if($var->isEmpty())
Do this
#else
Do that
#endif
From Laravel 5.4, you can also use the #isset directive.
#isset($variable)
{{-- your code --}}
#endisset
https://laravel.com/docs/9.x/blade#if-statements

Laravel - Check if #yield empty or not

Is it possible to check into a blade view if #yield have content or not?
I am trying to assign the page titles in the views:
#section("title", "hi world")
So I would like to check in the main layout view... something like:
<title> Sitename.com {{ #yield('title') ? ' - '.#yield('title') : '' }} </title>
For those looking on it now (2018+), you can use :
#hasSection('name')
#yield('name')
#endif
See : https://laravel.com/docs/5.6/blade#control-structures
In Laravel 5 we now have a hasSection method we can call on a View facade.
You can use View::hasSection to check if #yeild is empty or not:
<title>
#if(View::hasSection('title'))
#yield('title')
#else
Static Website Title Here
#endif
</title>
This conditional is checking if a section with the name of title was set in our view.
Tip: I see a lot of new artisans set up their title sections like this:
#section('title')
Your Title Here
#stop
but you can simplify this by just passing in a default value as the second argument:
#section('title', 'Your Title Here')
The hasSectionmethod was added April 15, 2015.
There is probably a prettier way to do this. But this does the trick.
#if (trim($__env->yieldContent('title')))
<h1>#yield('title')</h1>
#endif
Given from the docs:
#yield('section', 'Default Content');
Type in your main layout e.g. "app.blade.php", "main.blade.php", or "master.blade.php"
<title>{{ config('app.name') }} - #yield('title', 'Otherwise, DEFAULT here')</title>
And in the specific view page (blade file) type as follows:
#section('title')
My custom title for a specific page
#endsection
#hasSection('content')
#yield('content')
#else
\\Something else
#endif
see "Section Directives" in If Statements - Laravel docs
You can simply check if the section exists:
if (isset($__env->getSections()['title'])) {
#yield('title');
}
And you can even go a step further and pack this little piece of code into a Blade extension: http://laravel.com/docs/templates#extending-blade
Complete simple answer
<title> Sitename.com #hasSection('title') - #yield('title') #endif </title>
I have a similar problem with the solution:
#section('bar', '')
#hasSection('bar')
<div>#yield('bar')</div>
#endif
//Output
<div></div>
The result will be the empty <div></div>
Now, my suggestion, to fix this, is
#if (View::hasSection('bar') && !empty(View::yieldContent('bar')))
<div>#yield('bar')</div>
#endif
New in Laravel 7.x -- sectionMissing():
#hasSection('name')
#yield('name')
#else
#yield('alternative')
#endif
Check if section is missing:
#sectionMissing('name')
#yield('alternative')
#endif
#if (View::hasSection('my_section'))
<!--Do something-->
#endif
Use View::hasSection to check if a section is defined and View::getSection to get the section contents without using the #yield Blade directive.
<title>{{ View::hasSection('title') ? View::getSection('title') . ' - App Name' : 'App Name' }}</title>
I don't think you can, but you have options, like using a view composer to always provide a $title to your views:
View::composer('*', function($view)
{
$title = Config::get('app.title');
$view->with('title', $title ? " - $title" : '');
});
why not pass the title as a variable View::make('home')->with('title', 'Your Title') this will make your title available in $title
Can you not do:
layout.blade.php
<title> Sitename.com #section("title") Default #show </title>
And in subtemplate.blade.php:
#extends("layout")
#section("title") My new title #stop
The way to check is to not use the shortcut '#' but to use the long form: Section.
<?php
$title = Section::yield('title');
if(empty($title))
{
$title = 'EMPTY';
}
echo '<h1>' . $title . '</h1>';
?>
Building on Collin Jame's answer, if it is not obvious, I would recommend something like this:
<title>
{{ Config::get('site.title') }}
#if (trim($__env->yieldContent('title')))
- #yield('title')
#endif
</title>
Sometimes you have an enclosing code, which you only want to have included in that section is not empty. For this problem I just found this solution:
#if (filled(View::yieldContent('sub-title')))
<h2>#yield('sub-title')</h2>
#endif
The title H2 gets only displayed it the section really contains any value. Otherwise it won't be printed...

Laravel language handler in templates blade

How can i use something like {{#something}} and it will run a controller that checks for "something" so i can return it to translteable text?
My current blade template looks like following:
#layout("layouts.default")
#section("inner")
<h1>Velkommen til pornobiksen</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
I mean, how can i change the "Velkommen til pornobiksen" tekst? I know i can make something like
View::make("template")->with("h1_text","Velkommen til pornobiksen");
But is there not a module/plugin to make it easier? By making like {{#h1_text}} and it will takes from my database or something?
What is the easists way to make this?
You need to use {{ $h1_text }} to put the variable into your blade template.
#layout("layouts.default")
#section("inner")
<h1>{{ $h1_text }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
EDIT
I think I misunderstood you, it seems you are looking for localization
#layout("layouts.default")
#section("inner")
<h1>{{ Lang::get('messages.welcome') }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
For localization , you can use helper function : trans
home.php
return [
'welcome' => 'Velkommen til pornobiksen'
];
View
#layout("layouts.default")
#section("inner")
<h1>{{trans('home.welcome')}}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection

Categories