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
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 { 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; ?>'; }); } 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
But I will talk about this later.
Feedback, or criticism is always welcome :)