Brightside
Brightside

Reputation: 131

laravel 4 - how to show and hide components in blade?

I have breadcrumbs in my master.blade.php file and i want the breadcrumbs to be used everywhere BUT the homepage.blade.php.

right now this is how i add links to the breadcrumbs in other pages like "About". about.blade.php:

@section('breadcrumbs')
    @parent
    <li class="last-breadcrumb">
        About
    </li>
@stop

in the master.blade.php:

<div class="container">
    <div class="breadcrumb-container">
        <ul class="breadcrumb">
            @section('breadcrumbs')
            <li>
                <a href="/homepage/" title="homepage">
                    homepage
                </a>
            </li>
            @show
        </ul>
    </div>
</div>

but i don't want the breadcrumbs code to display at all when homepage.blade been used.

copying the code for each about.blade/X.blade files seems like a bad idea..

Upvotes: 1

Views: 10128

Answers (3)

sidneydobber
sidneydobber

Reputation: 2910

Your can set a value in your controller that you pass with the make/redirect like $data['breadcrumb'] = true and wrap your breadcrumb code in an conditional. This kind of system also works well for error and success messages since you can alse send content from your controller. Then you would send an array with values instead of true.

Blade template

<div class="container">
    @if($breadcrumb)
    <div class="breadcrumb-container">
        <ul class="breadcrumb">
            @section('breadcrumbs')
            <li>
                <a href="/homepage/" title="homepage">
                    homepage
                </a>
            </li>
            @show
        </ul>
    </div>
    @endif
</div>

Upvotes: 1

edcs
edcs

Reputation: 3879

If you use @yield in your master template then it will only display content when the section has been defined. @content requires the section to be always defined.

Upvotes: 1

user1669496
user1669496

Reputation: 33068

You can check the URI to see if you want to display it or not. It's logic in your view which is usually frowned upon, but it's the easiest way I can come up with.

<div class="container">
    <div class="breadcrumb-container">
        <ul class="breadcrumb">
            @if(Route::uri() == '/')
            <li class="last-breadcrumb">
                About
            </li>
            @endif
            <li>
                <a href="/homepage/" title="homepage">
                    homepage
                </a>
            </li>
            @show
        </ul>
    </div>
</div>

Depending on the URI you are using for your home page, you may need to tweak the condition.

Upvotes: 1

Related Questions