📜 ⬆️ ⬇️

Add so missing parse_string ()

image In March, news came from the Wave Habrayuser that made the hearts of all CodeIgniter framework lovers thrill: version 2.0 of this lyalka is about to appear. And now, more than two months have already passed, and the cart is still unavailable for downloading from off.site.

And everything would be fine, but there was a great lack of a method that they promised to introduce into the second version of the framework - parse_string () in the Parser class, which would allow us to parse templates not only from files, but also from variables. And so, as in one of my developments there is a need to store small templates in the database, then, so to speak, it became unbearable to me.

Praise to the developers, we were given the opportunity to expand all the standard classes, which we will use.
')
For the implementation of this method - welcome to habrokat.

So, in order to extend the functionality of the Parser class, create the file " MY_Parser.php " in the directory " application / libraries ". Insert the following code inside the file:

<?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 .
  1. <?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 .
  2. <?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 .
  3. <?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 .
  4. <?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 .
  5. <?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 .
  6. <?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 .
  7. <?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 .
  8. <?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 .
  9. <?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 .
  10. <?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 .
  11. <?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 .
  12. <?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 .
  13. <?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 .
  14. <?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 .
  15. <?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 .
  16. <?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 .
  17. <?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 .
  18. <?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 .
  19. <?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 .
  20. <?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 .
  21. <?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 .
  22. <?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 .
  23. <?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 .
  24. <?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 .
  25. <?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 .


That's all! Now the new parse_string () method will be accessible from anywhere, no matter where you connect the Parser library.

Parameters this method takes almost identical to what the parse method asks for, except that the first parameter, instead of the path to the template file, is a string variable containing the template text. The second parameter is also an array with variables, and the third parameter indicates how the method returns the processed template — return to a variable, or directly to output.

I hope someone this small add-on for CodeIgniter 1.7.2 will be useful while waiting for a full 2.0 release.

See you again;)

Source: https://habr.com/ru/post/95167/


All Articles