티스토리 뷰

코드이그나이터에서 블레이드 템플렛 사용하기


우선 블레이드를 라라벨 없이 사용가능한 컴포넌트를 받아 오토로드 합니다.

컴포저가 설치됐다는 가정 하에 커멘드라인에서 컴포넌트 설치. 

 
composer require duncan3dc/blade
 

패키지스트 : https://packagist.org/packages/duncan3dc/blade 

깃허브 : https://github.com/duncan3dc/blade 

컴포저 오토로드 후.

블레이드 컴포넌트를 CI 컨트롤러 안에서 사용할 수 있게 라이브러리를 만듭니다.


<라이브러리 예제>

<?php
defined("BASEPATH") OR exit("No direct script access allowed");
 
use duncan3dc\Laravel\BladeInstance;
use duncan3dc\Laravel\Blade as coreBlade;
 
class Blade extends coreBlade {
 
    public static $ci;
 
    public function __construct()
    {
        self::$ci =& get_instance();
 
        // view파일 경로, cache파일 경로를 지정하기 위해 BladeInstance 객체 생성 후 코어 클레스에 인스턴스 지정
        // ex) BladeInstance(view 경로: application/views, cache 경로: application/cache/balde/views)
        // Blade 문법으로 작성된 view는 렌더링 후 cache 경로에 php문법으로 변환하여 저장된다.
        // cache 경로는 777 or 707 권한부여
        parent::setInstance(new BladeInstance(APPPATH . "views", APPPATH . "cache/blade/views"));
    }
 
    public static function __callStatic($method, array $args)
    {
        // render method는 실행 전 파라미터 값 무결성, Blade view 파일 존재 유무 확인.
        if ($method == "render") {
            if (is_array($args) && count($args) == 2 && is_string($args[0]) && !empty($args[0]) && is_array($args[1])) {
                if (file_exists(APPPATH . "views/" . $args[0] . ".blade.php")) {
                    // 렌더링된 view 정보를 CI output 결과에 주입.
                    return self::$ci->output->set_output(parent::render(...$args));
                } else {
                    return self::$ci->output->set_content_type("text/html")->set_output("<p>blade view 파일이 없습니다.</p>");
                }
                 
            } else {
                return self::$ci->output->set_content_type("text/html")->set_output("<p>파라미터 값이 잘못 입력되었습니다.</p>");
            }
        } else {
            // render method를 제외한 method는 바로 실행.
            return parent::$method(...$args);
        }
    }
}

컨트롤러에 로드 후 사용


<컨트롤러 예제>

<?php // controller
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Ctrler extends CI_Controller
{
 
    public function __construct()
    {
        $this->load->library->("blade");
    }  
 
    public function index($data)
    {
        $data["ex_var"] = "예제변수";
        blade::render("view", $data); // view.blade.php
    }
}

<뷰 예제>

// ------ view.blade.php
 
@extends('layouts.app')
 
@section('title', 'Page Title')
 
@section('sidebar')
    @parent
 
    <p>This is appended to the master sidebar.</p>
    {{ blade::$ci->session->userdata("something") }}
    {{ blade::$ci->input->get("something") }}
@endsection
 
@section('content')
    <p>This is my body content.</p>
    {{ $ex_var }}
@endsection

<레이아웃 예제>

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show
        {{ blade::$ci->session->userdata("something") }}
        {{ blade::$ci->input->get("something") }}
        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

  그리고 라라벨 블레이드 템플레트 엔진 메뉴얼 입니다. 


'Programming Framework > CodeIgniter' 카테고리의 다른 글

Codeigniter3 XSS filtering issue  (0) 2019.08.13
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함