ひらい ぶらり Hi-Library

ぷろぐらみんぐについて。ときどきどうでもいいことについて。

CodeIgniter2.0.3 以降でSmarty3.0を使う

CodeIgniter2.0.3以降からは、system/core/Loader.phpのCI_Loaderクラスで「_ci_cached_vars」がprotectedになってしまったので、僕が以前書いた記事のライブラリでは以下のようなエラーが出てしまう

Cannot access protected property MY_Loader::$_ci_cached_vars

CI_Loaderクラスを覗いてみると、アンダースコアから始まる変数は全部protectedになり、必要に応じて取得するメソッドが作られたようだ。
ただし_ci_cached_varsを取得するようなメソッドは見当たらない。

orz

仕方ないので application/core/MY_Loader.phpを作る。

application/core/MY_Loader.php

<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Loader extends CI_Loader
{
    public function get_ci_cached_vars()
    {   
        return $this->_ci_cached_vars;
    }   
}

次いで、MY_Parser.phpを修正する
$this->CI->load->_ci_cached_varsに直接アクセスしている部分を、get_ci_cached_vars()でアクセスするように変更。

application/library/MY_Parser.php

<?php
    public function parse($template, $data = '', $return = FALSE, $use_theme = FALSE)
    {  
        // Make sure we have a template, yo.
        if ($template == '')
        {   
            return FALSE;
        }   

        if ($this->CI->agent->is_smartphone()) {
            $template = 's/' . $template;
        }   
    
        // If no file extension dot has been found default to .php for view extensions
        if ( !stripos($template, '.') ) 
        {   
            $template = $template.".".$this->CI->smarty->template_ext;
        }   
        
        // Merge in any cached variables with our supplied variables
        if (is_array($data))
        {   
            //$data = array_merge($data, $this->CI->load->_ci_cached_vars);
            $data = array_merge($data, $this->CI->load->get_ci_cached_vars());
        }   
        
        // If we have variables to assign, lets assign them
        if ($data)
        {   
            foreach ($data as $key => $val)
            {   
                $this->CI->smarty->assign($key, $val);
            }   
        }   

        $this->CI->smarty->assign('CI', $this->CI);

        // Get our template data as a string
        $template_string = $this->CI->smarty->fetch($template);

        // If we're returning the templates contents, we're displaying the template
        if ($return == FALSE)
        {
            $this->CI->output->append_output($template_string);
        }

        // We're returning the contents, fo'' shizzle
        return $template_string;
    }

動いた動いた。
めでたしめでたし。