そして、すべては大丈夫ですが、フレームワークの2番目のバージョン、Parserクラスのparse_string()で実装されると約束されたメソッドは、ファイルからだけでなく変数からもテンプレートを解析できるようにします。 そして、私の開発の1つでは、小さなテンプレートをデータベースに保存する必要があるため、いわば、私にとっては耐え難いものになりました。
開発者を称賛し、使用するすべての標準クラスを拡張する機会を与えられました。
このメソッドの実装について-Habrokatへようこそ。
そのため、Parserクラスの機能を拡張するには、ディレクトリ「 application / libraries 」にファイル「 MY_Parser.php 」を作成します。 ファイル内に次のコードを挿入します。
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
<?php class MY_Parser extends CI_Parser{ function parse_string($ string = "" ,$values = array(),$ return = false ){ foreach ($values as $key => $val){ if (is_array($val)) { $ string = $ this ->_parse_pair($key, $val, $ string ); } else { $ string = $ this ->_parse_single($key, ( string )$val, $ string ); } } $CI =& get_instance(); ob_start(); foreach ($values as $key => $val) $$key = $val; echo eval( '?>' .preg_replace( "/;*\s*\?>/" , "; ?>" , str_replace( '<?=' , '<?php echo ' , $ string ))); $ string = ob_get_contents(); @ob_end_clean(); if ($ return ) return $ string ; $CI->output->append_output($ string ); } } ?> * This source code was highlighted with Source Code Highlighter .
以上です! これで、新しいparse_string()メソッドは、Parserライブラリを接続する場所であればどこからでも使用できるようになります。
このメソッドは、解析メソッドによって要求されたものとほぼ同じパラメーターを受け入れますが、テンプレートファイルへのパスの代わりに最初のパラメーターがテンプレートテキストを含む文字列変数である点が異なります。 2番目のパラメーターも変数を含む配列であり、3番目はメソッドが処理済みのテンプレートを返す方法を示します-変数に返すか、直接出力します。
CodeIgniter 1.7.2へのこの小さな追加が、完全な2.0リリースを待っている間に役立つことを願っています。
また会うまで;)