0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PHP】Laravel bladeビューファイルの記述の注意点

Last updated at Posted at 2020-12-24

個人メモです。
Laravelでbladeビューファイルの記述の注意点について。

注意点

  • PHPの処理は全て冒頭に@が必要
  • PHP処理は1行ずつ(改行は使えない)
  • ディレクティブがない行はHTMLとみなされる。
  • PHPの処理のみを改行ありで記述する場合は@phpを使う
  • PHP処理の終わりは@end処理の名前を記述

## bladeビューとは? 拡張子`.blade.php`のファイル。 HTMLをそのまま記述することができる。冒頭に@をつけることでphpの処理も併せて記述できる。
xxx.blade.php
<div>
 @if(条件式)
   <p> true </p>
 @endif
</div>

コンパイル時に@があればPHPと見なす。


## PHPの処理は全て冒頭に`@`が必要 if文の処理を使う場合は、`@if`と`@endif`だけではなく、`@else`など、PHPの行の冒頭には全て@が必要。
OK
<div>
 @if(条件式)
   <p> true </p>
 @elseif(条件式2)
   <p> else if</p>
 @else
   <p> false </p>
 @endif
</div>

@がない行はHTMLとみなされる。
NG(エラー)
<div>
 @if(条件式)
   <p> true </p>
 elseif(条件式2)
   <p> else if</p>
 else
   <p> false </p>
 @endif
</div>

## PHP処理は1行ずつ(改行は使えない) @がない行はHTMLとみなされるため、`if(条件式){処理}`の`{ }`は使えない。
不可
<div>
 @if(条件式) {
     処理
    }
 @endif
</div>

## PHPの処理のみを改行ありで記述する場合は`@php`を使う 普通のPHPの処理のように`{ }`も使って記述したい場合`@php`と`@endphp`の中に記述する。
@php
 if(条件式) {
     処理
    }
 endif
@endphp

@phpの中のはphpの世界のため、@は不要(@ifの@はいらない。)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?