在codeigniter中使用blade模板引擎

Auth:admin Date:2016-06-4 17:43:00 Cat:技术笔记


使用compoer引入blade库

composer require "philo/laravel-blade": "3.*"

在helpers目录下创建 view_helper.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once 'vendor/autoload.php';

use Philo\Blade\Blade;

if (!function_exists('view')) {
    function view($name = NULL, $data = [], $mergeData = [])
    {
        $CI = &get_instance();
        if (!isset($CI->blade)) {
            $views = __DIR__ . '/../views';
            $cache = __DIR__ . '/../cache';
            $CI->blade = new Blade($views, $cache);
            $elapsed_time = $CI->benchmark->elapsed_time('total_execution_time_start', 'total_execution_time_end');
            $CI->blade->view()->share('elapsed_time', $elapsed_time);
        }
        echo $CI->blade->view()->make($name, $data, $mergeData)->render();
    }
}

在config/autoload.php中,引入 

$autoload['helper'] = array('view', 'url');

controller中:

<?php defined('BASEPATH') or exit('No direct script access allowed');
class Welcome extends CI_Controller
{
    public function index()
    {
        return view('index', ['name' => 'haha']);
    }
    public function test()
    {
        echo 'this is a test';
    }
}

还是喜欢codeigniter的简洁和可扩展性,用来做中小项目还是很爽的。

标签:,