0

Since when I used smarty I always tried to separate logic statements from html tags, so my approach to blade would be to write:

template.blade.php

<div>
    <div>
@if ($something == true)
        <ul>
    @foreach($list as $item)
            <li>
                {{ $item->name }}
            </li>
    @endforeach
        </ul>
@endif
    </div>
</div>

But the normal way I see this written is:

template.blade.php

<div>
    <div>
        @if ($something == true)
            <ul>
                @foreach($list as $item)
                    <li>
                        {{ $item->name }}
                    </li>
                @endforeach
            </ul>
        @endif
    </div>
</div>

But also sometimes directive is on the same column as preceding tag and sometimes tags are in the same columns as preceding directive.

I know is pointless to force my style if everybody else use a different one, no matter how much more sense makes to me, but is there any standard from Laravel?

  • I think it's better to write like the normal way you see. With the normal way you can sorting out all confusing lines of code. – Josua Marcel Chrisano Apr 3 '18 at 11:12
3

This often relies on preferences. BLADE doesn't have any special coding standards that differ from standard PHP in HTML. If you work in a company, there might be some standard set to keep the code consistent. Personally, I would advise to follow the normal way or to put your blade tags like this:

<div>
    <div>
    @if($something == true)
         <ul>
         @foreach($list as $item)
             <li>
             {{ $item->name }}
             </li>
         @endforeach
         </ul>
    @endif
    </div>
</div>

That way your html and blade tags flow in their own way, but it stays readable.

  • Thanks for your input, looks better than adding a tab after everything. Also I wanted to point out that there should be no space between @if and () that was my mistake, I guess you just did copy and paste. – Crayon May 18 '18 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.