Skip to content

Instantly share code, notes, and snippets.

@adamtester
Created May 9, 2013 08:35
Show Gist options
  • Save adamtester/5546300 to your computer and use it in GitHub Desktop.
Save adamtester/5546300 to your computer and use it in GitHub Desktop.
Extends the CI Parser class to allow for language files to be parsed. With this instead of using <?php echo $this->lang->line('lineoftext'); ?> You can now use an underscore in your view files like so: {_lineoftext}. Enjoy cleaner view files!
<?php
/*
******************************************************************************
// Extends the CI Parser class to allow for language files to be parsed.
// With this instead of using <?php echo $this->lang->line('lineoftext'); ?>
// You can now use an underscore in your view files like so: {_lineoftext}
// Enjoy cleaner view files!
// Place this file in application/libraries/MY_Parser.php
******************************************************************************
*/
class MY_Parser extends CI_Parser {
const LANG_REPLACE_REGEXP = '!\{_\s*(?<key>[^\}]+)\}!';
static $CI = null;
public function parse($template, $data, $return = FALSE) {
$this->CI = get_instance();
$template = $this->CI->load->view($template, $data, TRUE);
$template = $this->replace_lang_keys($template);
return $this->_parse($template, $data, $return);
}
protected function replace_lang_keys($template) {
return preg_replace_callback(self::LANG_REPLACE_REGEXP, array($this, 'replace_lang_key'), $template);
}
protected function replace_lang_key($key) {
return $this->CI->lang->line($key[1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment