Smart way to hide any item from any view in Laravel 5.3?

I had a problem when I do not want to display the X element in some views.

The real example is that I do not want to show the product image slider on the contact and about us pages, but it should be visible on all other pages (about 6 more)

So, how do I exclude the rendering of a div in n pages m without the very long @if conditions? Any idea for a stylish solution?

Edit When asked why I do not want to put this in a stand-alone file of the form:
Imagine that you want this line to be visible in all views of your views, except for a certain one, and only one

 <span>Lorem Ipsum</span> 

This is just one line of HTML code, you should not create another file in the project, because it will be a mess in the project file tree, and another example is a bad workaround

 @if(\Request::route()->getName() !== "some.very.long.route.alias.name.that.looks.ugly") <span>Lorem Ipsum</span> @endif 

Makes code difficult to read and understand when a project grows.

Edit 2 I predicted some of the proposed solutions and already wrote why this is not suitable for this. I'm more likely to look at a solution similar to @can or @cannot , which depends on the current route

Edit 3: Work on the solution

Edit 4: I posted an answer here, but it’s not perfect yet (I won’t mark it as usual), so for readers of this topic I will put a copy of it below

Ok, I made a simple left directive that solves this case in a good looking way

First create an array defining access to the route for html snippet
It would be best to create this file in the config directory, for example, the presence.php file

 <?php return [ 'ipsum' => [ // presence alias 'about', // disallowed route #1 'contact' // disallowed route #2 ], ]; 

Then create a directive in the AppServiceProvider.php class

 <?php namespace App\Providers; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Blade::directive('presence', function($alias) { return "<?php if (in_array(\Request::route()->getName(),config('presence.'.$alias))):?>"; }); Blade::directive('endpresence', function() { return '<?php endif; ?>'; }); } /** * Register any application services. * * @return void */ public function register() { // } } 

And in the final use of this trick in html view

 @presence('ipsum') <span>Lorem Ipsum</span> @endpresence 

It will print Lorem Ipsum if the name of the current route is not specified in the ipsum array (which is declared in the \App\config\presence.php file, so we can access it using the config() helper method)

It would be great if I could handle wildcards like

 <?php return [ 'ipsum' => [ // presence alias 'admin.category.*', // advanced route pattern #1 'admin.users.*' // advanced route pattern #2 ], ]; 

But I will talk about this later.

Feedback, or criticism is always welcome :)

+5
source share
4 answers

Well, I developed a simple directive with blades that correctly solves this case.

First create an array defining access to the route for html snippet
It would be best to create this file in the config directory, for example, presence.php file

 <?php return [ 'ipsum' => [ // presence alias 'about', // disallowed route #1 'contact' // disallowed route #2 ], ]; 

Then create a directive in the AppServiceProvider.php class

 <?php namespace App\Providers; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Blade::directive('presence', function($alias) { return "<?php if (in_array(\Request::route()->getName(), config('presence.'.$alias))): ?>"; }); Blade::directive('endpresence', function() { return '<?php endif; ?>'; }); } /** * Register any application services. * * @return void */ public function register() { // } } 

And in the final use of this trick in html view

 @presence('ipsum') <span>Lorem Ipsum</span> @endpresence 

It will print Lorem Ipsum if the current route name is not in the ipsum array (which is declared in the \App\config\presence.php file, so we can access it using the config() helper method)

It would be great if I could handle wildcards like

 <?php return [ 'ipsum' => [ // presence alias 'admin.category.*', // advanced route pattern #1 'admin.users.*' // advanced route pattern #2 ], ]; 

But I will talk about this later.

Feedback, or criticism is always welcome :)

0
source

If you use clip templates, you can create two different layouts. One with an image slider and one without it. This is the simplest solution I can come up with with my head.

EDIT: Creating another layout, especially if you have all the layouts in a separate folder, will not create clutter in the file structure, and it is definitely worth it.

+1
source

If contact and about us have their own URIs, you can try matching them to determine if the not ... element should be shown.

 if (!\Request::is('contact') && !\Request::is('about-us')) { // Show element X } 

Or another way to do this would be to add middleware that sets some variable, like $dontShowElement , and sets the composer of the view and passes that variable to all the views. Then all you have to do is check for the presence of this variable before outputting the element and provide only those 2 routes that you want this element to be hidden in this middleware.

0
source

copy this to App / Http / helper.php

 function is_path($path){ return call_user_func_array('Request::is', (array)$path) ? true : false; } 

View

 @if(is_path('about')) <h1>About</h1> @endif Mai this will help you 
0
source

Source: https://habr.com/ru/post/1258423/


All Articles