Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Developer654079525's avatar

Conditional include in a blade sub-template

I have a main template that includes the sub-template via:

@include('heading')

The heading sub-template serves the content based on the route's name:

@if(Route::currentRouteName() === 'index')
  // index heading content
@endif
@if(Route::currentRouteName() === 'about')
  // about heading content
@endif
@if(Route::currentRouteName() === 'contact')
  // contact heading content
@endif
// more @if @endifs below

Is there a more efficient way of conditionally including the content instead of having multiple parallel @if statements?

0 likes
1 reply
Shivamyadav's avatar
Level 20

Look for this 1st parameter is Boolean as you have passed the if Boolean condition and then view name and 3rd parameter if you want to pass any data to the view..

If you would like to @include a view if a given boolean expression evaluates to true or false, you may use the @includeWhen and @includeUnless directives:

@includeWhen($boolean, 'view.name', ['status' => 'complete'])
 
@includeUnless($boolean, 'view.name', ['status' => 'complete'])

Note ___

@includeWhen($condition, 'view'): Includes the view when the condition is true.

@includeUnless($condition, 'view'): Includes the view when the condition is false
1 like

Please or to participate in this conversation.