Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Inelegant Blade syntax
Is there no more elegant way to do a conditional class in Blade?
<ul class="nolist">
@foreach ($docs_versions as $doc_key => $doc_version)
<li
@if (DOCS_VERSION == $doc_key)
class="current"
@endif
><a href="{{ url('docs/' . $doc_version['url_key']) }}" title="{{ $doc_version['title'] }}">{{ $doc_version['title'] }}</a></li>
@endforeach
</ul>
@mattstauffer

This comment has been minimized.

Copy link
Owner Author

@mattstauffer mattstauffer commented Jul 30, 2014

@ericbarnes suggested ternary. I like it!

            <ul class="nolist">
                @foreach ($docs_versions as $doc_key => $doc_version)
                    <li class="{{{ DOCS_VERSION == $doc_key ? 'current' : '' }}}">
                        <a href="{{ url('docs/' . $doc_version['url_key']) }}" title="{{ $doc_version['title'] }}">{{ $doc_version['title'] }}</a>
                    </li>
                @endforeach
            </ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment