// klasa class st_parser // simple template pareser { var $text, $const_rplc, $const_loop_start, $const_loop_stop; function st_parser($file_name = '') { $this->const_rplc = 'RPLC'; $this->const_loop_start = 'LOOP_START'; $this->const_loop_stop = 'LOOP_STOP'; if ($file_name == '') return true; $tmp = ($tmp = file($file_name)) ? $tmp : array(); $this->text = join('', $tmp); } function replace($mixed_arg) { $arg1 = $arg2 = array(); if (!is_array($mixed_arg)) $mixed_arg = func_get_args(); for ($i = 0; $i < count($mixed_arg); $i++) { $arg1[] = ''; $arg2[] = $mixed_arg[++$i]; } $this->text = str_replace($arg1, $arg2, $this->text); } function loop_replace($loop_id, $a_rplc, $aa_val) { foreach($a_rplc as $k => $v) $a_rplc[$k] = ''; $reg = "|(.*)|Us"; if (!preg_match($reg , $this->text, $matches)) return false; foreach($aa_val as $k => $v) $str .= str_replace($a_rplc, $v, $matches[1]); $this->text = preg_replace($reg, $str, $this->text, 1); } function show() { print $this->text; } } // przykład uzycia $stp = new st_parser('./tpls/jakis_plik.tpl'); $stp->replace( 'PAGE_TITLE', $page_title, 'MENU', $menu, 'BODY', $body //itd... ); // [CUT] ... while ($tmp = mysql_fetch_row($ret)) $tmp_array[] = $tmp; $stp->loop_replace( 'GB_LIST', array('REC_ID', 'TITLE', 'DATA', 'TRESC'), $tmp_array ); // [CUT] ... $stp->show(); // wybrane przyklady z pliku *.tpl