Home 라라벨로 배우는 php 웹 프로그래밍
Post
Cancel

라라벨로 배우는 php 웹 프로그래밍

라라벨로 배우는 실전 PHP 웹 프로그래밍

PART 1 라라벨 입문

5. 블레이드

블레이드는 아아벨의 템플릿 언어인 동시에 HTML 컴파일 엔진이다. 템플릿 언어는 다음의 특징을 가진다.

  • 변수를 이용한 문자열 보간(string interpolation)
  • 제어구조(control structure)
  • 템플릿 상속
  • 조각(portal) 뷰 삽입

블레이드 엔진은 템플릿을 PHP 스크립트로 컴파일하고, 이렇게 변환된 PHP파일을 PHP엔진이 다시 HTML로 컴파일한다.
블레이드 문법으로 쓴 템플릿은 .blade.php 확장자를 가져야 하며, resources/views 디렉터리 아래에 저장해야 한다.


5.1 변수를 이용한 문자열 보간

resources/views/welcome.blade.php ( 5.7 이하)

1
<h1>{{ $greeting or 'Hello' }} {{ $name or ''}}</h1>  

resources/views/welcome.blade.php ( 5.7 이상)

1
<h1>{{ $greeting ?? 'Hello' }} {{ $name ?? ''}}</h1>  

블레이드는 문자열 보간을 위해 이중 중괄호({{}})를 이용한다. {{ $name}}<?= $name; ?> php 문법과 같다. {{ $greeting or 'Hello '}}<=? isset($greeting) ? "{$greeting}" : 'Hello'; ?> 와 같다. 이 문법을 사용할 때는 {{$greeting}}처럼 써도 된다. 중괄호와 변수 사이의 공백은 개인의 취향이다.

블레이드는 XSS 공격으로부터 서비스를 보호하기 윈해 문자열을 보간한 때 특수 문자를 이스케이프한다. 이스케이프하지 않은 채로 문자열을 뷰에 포함하려면 {!! $var!!}문법을 이용한다.

몇몇 자바스크립트에서 프레임워크도 {{}}를 문자열 보간 문법으로 사용한다. 브레이드 내의 자바스크립에서 문자열 보간을 사용한다면 @{{}} 문법을 사용해야 한다.

5.2 주석

블레이드에서 주석은 {{--주석--}} 형식을 사용한다.

resources/views/welcome.blade.php

1
2
{{--HTEML 주석으로 표시 됩니다.--}}
<h1>{{ $greeting or 'Hello' }} {{ $name or ''}}</h1>  

5.3 제어 구조

블레이드는 모든 제어 구조에 이메일기호(@)를 이용하고, end로 시작하는 키워드로 제어 구조의 끝을 표시한다.

조건문

routes/web.php

1
2
3
4
Route::get('/', function () {
    $items = ['사과','포도','딸기'];
    return view('welcome',['items' => $items]);
});

resources/views/welcome.blade.php

1
2
3
4
5
@if ($itemCount = count($items))
    <p>{{$itemCount}} 종류의 과일이 있습니다.</p>
@else
    <p>아무것도 없습니다.</p>
@endif

@elseif , @unless(조건식) 도 사용 가능하다. * @unless는 @if(! 조건식)과 의미가 같다

반복문

resources/views/welcome.blade.php

1
2
3
4
5
6
<ul>
@foreach ($items as $item)
    <li>{{ $item }}</li>
@endforeach
</ul>

@for, @while 도 사용가능하며 배열값이 없을 경우, 처리하는 @forelse도 있다. <?php $items = [] ?>와 같이 호출되는 블레이드에서 변수에 할당된 값을 오버라이딩 할 수 있다.

resources/views/welcome.blade.php

1
2
3
4
5
6
7
<ul>
@forelse ($items as $item)
    <li>{{ $item }}</li>
@empty
    <li>값이 없습니다.</li>
@endforelse
</ul>

5.4 템플릿 상속

최상위 부모 템플릿인 마스터레이아웃을 만들어 보자. resources/views/layouts 디렉터리를 만들고 코드를 작성한다.

resources/views/layouts/master.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>라라벨 입문</title>
</head>
<body>

    @yield('content')
</body>
</html>

마스터 레이아웃을 상속 받도록 welcome.blade.php를 수정한다.

resources/views/welcome.blade.php

1
2
3
4
5
@extends('layouts.master')

@section('content')
    <p>저는 자식 뷰의 컨텐츠 섹션입니다.</p>
@endsection

@yield(섹션명) 으로 해당 부모의 레이아웃에 자식의 내용을 표출 가능하다.

5.5 조각 뷰 삽입

블레이드로 정의된 뷰에서 다른 조각 뷰를 가져다가 삽입 가능하다. 조각뷰를 만들어 본다

resources/views/parials/footer.blade.php

1
2
3
<footer>
    <p>저는 꼬리말입니다. 다른 뷰에서 저를 사용합니다.</p>
</footer>

다른 뷰에서 @include를 이용해서 사용한다.

resources/views/welcome.blade.php

1
2
3
4
5
@extends('layouts.master')

@section('content')
    @include('partials.footer')
@endsection

섹션 명이 중복될 경우

resources/views/layouts/master.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>라라벨 입문</title>
</head>
<body>

    @yield('content')
    @yield('script')
</body>
</html>

resources/views/parials/footer.blade.php

1
2
3
4
5
@section('script')
    <script>
        alert("조각뷰의 섹션입니다.");
    </script>
@endsection

resources/views/welcome.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
@extends('layouts.master')

@section('content')
    @include('partials.footer')
@endsection

@section('script')
    <script>
        alert("자식 뷰의 섹션입니다");
    </script>
@endsection

조각뷰의 섹션입니다가 호출 된다.
둘다 호출하려면 조각뷰쪽 코드를 수정한다

resources/views/parials/footer.blade.php

1
2
3
4
5
6
@section('script')
    @parent
    <script>
        alert("조각뷰의 섹션입니다.");
    </script>
@endsection
This post is licensed under CC BY 4.0 by the author.

라라벨로 배우는 php 웹 프로그래밍

라라벨로 배우는 php 웹 프로그래밍

Comments powered by Disqus.

Trending Tags