Search code examples
phplaravellaravel-bladelaravel-3

Laravel - Blade Template define sections/include


Is there a way I don't have to use multiple @include for the tabs for some pages.

For example I do something like this

In the section/product-tab1.blade.php

<ul class="nav-tabs hidden-xs" role="tablist">
   <li><a href="/something1" role="tab">Category Name 1</a></li>
   <li class="active"><a href="/something2">Category Name 2</a></li>
</ul>

In the section/product-tab2.blade.php

<ul class="nav-tabs hidden-xs" role="tablist">
   <li><a href="/something4" role="tab">Something Name 4</a></li>
   <li class="active"><a href="/something5">Something  Name 5</a></li>
   <li class="active"><a href="/something6">Something Name 6</a></li>
</ul>

In the something1.blade.php I would define like this to show tabs:

 @include('product-tab1')

In the something4.blade.php show tabs:

 @include('product-tab2')

Also would you use if statement in the view file to define class="active" or not? It will get defined variable from the controller.


Solution

  • You would use an if conditional, a short-if is in fact ver short in this case. An if for a navigation could look something like this (notice this if with Bootstrap classes, you probably want to use different classes).

    Ideally you should write a helper for this, but we will just do it with a bit of logic in your view for now. Simply go to the tag you want to set the active class on and put the following in.

    class="{{ Request::is('your/path') ? 'active' : '' }}"
    

    Now that is kind of dirty, but it most certainly works. You should create a helper for it eventually, but you really don't need one for testing or small projects.

    A great resource for stuff like this is Laracasts, here is the corresponding video:

    https://laracasts.com/lessons/active-states


    Now for your other issue, I don't know exactly what you are doing or why you referencing those tabs. It's really hard to come up with a good solution this way.