TIP게시판

제목 라라벨의 블레이드 템프레이트 엔진을 가져와 적용해 봤습니다.
글쓴이 호짱 작성시각 2017/09/13 08:36:30
댓글 : 3 추천 : 0 스크랩 : 0 조회수 : 15433   RSS

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

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

$ 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>

그럼 불코하세요~

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

https://laravel.kr/docs/5.5/blade

 

오류나 부족한 부분있으면 댓글주세요~

http://khanorder.tistory.com/entry/ci-blade

태그 템플레이트 엔진,블레이드,라라벨
관련링크 https://github.com/duncan3dc/blade
https://laravel.kr/docs/5.5/blade
http://khanorder.tistory.com/entry/ci-blade
 다음글 post 한글 일본어 utf-8 깨짐 (1)
 이전글 세션 redirect 끊김 (1)

댓글

곰멍 / 2017/09/13 08:39:17 / 추천 0
좋은 정보 감사합니다~
한대승(불의회상) / 2017/09/13 08:39:24 / 추천 0

라라벨 블레이드 템플릿 기능을 코드이그나이터에서도 사용 가능하게 되었네요. ^^

좋은 정보 감사 합니다.

변종원(웅파) / 2017/09/13 09:06:45 / 추천 0
정보 감사합니다.