A simple tag parsing library.
/** * Get the data pertaining to the given double tag. This will * loop through arrays of given data * * Example Data: * $data = array( * 'books' => array( * array( * 'title' => 'PHP for Dummies', * 'author' => 'John Doe' * ), * array( * 'title' => 'CodeIgniter for Dummies', * 'author' => 'Jane Doe' * ) * ) * ); * * Example Tags: * {books} * {title} by {author}<br /> * {/books} * * @access private * @param array The double tag * @param array The data to parse * @return mixed Either the data for the tag or FALSE */ private function _parse_data_double($tag, $data) { $return_data = ''; foreach ($tag['segments'] as $segment) { if (!isset($data[$segment])) { return FALSE; } $data = $data[$segment]; } $temp = new Simpletags(); foreach ($data as $val) { $return = $temp->parse($tag['content'], $val); $return_data .= $return['content']; } unset($temp); return $return_data; }
/** * Callback from template parser * * @param array * @return mixed */ public function parser_callback($data) { if (!isset($data['segments'][0]) or !isset($data['segments'][1])) { return FALSE; } // Setup our paths from the data array $class = $data['segments'][0]; $method = $data['segments'][1]; $addon = strtolower($class); $return_data = ''; if ($class == 'site') { $addon_path = APPPATH . '/libraries/parsers/Site_parser.php'; if (!file_exists($addon_path)) { $return = FALSE; } $this->_ci->load->library('parsers/Site_parser', $data); $return_data = $this->_process('site_parser', $method, $data); } elseif (in_array($class, $this->add_ons)) { $addon_path = EXTPATH . $class . '/libraries/' . $class . EXT; if (!file_exists($addon_path)) { log_message('error', 'Unable to load: ' . $class); $return = FALSE; } else { // Load that library $this->_ci->load->library($class . '/' . $class, $data); // How about a language file? $lang_path = EXTPATH . $class . '/language/' . $this->_ci->config->item('language') . '/' . $addon . '_lang' . EXT; if (file_exists($lang_path)) { $this->_ci->lang->load($addon . '/' . $addon); } // Now the fun stuff! $return_data = $this->_process($class, $method, $data); } } else { // Now we are going to check the core "modules" and see if this is what they want $addon_path = APPPATH . 'modules/' . $class . '/libraries/' . ucfirst($class) . '_parser' . EXT; if (!file_exists($addon_path)) { $addon_path = APPPATH . 'modules/kb/libraries/' . ucfirst($class) . '_parser' . EXT; if (!file_exists($addon_path)) { log_message('error', 'Unable to load: ' . $class); $return = FALSE; } else { $this->_ci->load->library('kb/' . $class . '_parser', $data); $return_data = $this->_process($class . '_parser', $method, $data); } } else { $this->_ci->load->library($class . '/' . $class . '_parser', $data); $return_data = $this->_process($class . '_parser', $method, $data); } } if (is_array($return_data) && !empty($return_data)) { if (!$this->_is_multi($return_data)) { $return_data = $this->_make_multi($return_data); } $content = $data['content']; $parsed_return = ''; $simpletags = new Simpletags(); foreach ($return_data as $result) { $parsed = $simpletags->parse($content, $result); $parsed_return .= $parsed['content']; } unset($simpletags); $return_data = $parsed_return; } return $return_data; }