Новичок
Покинул форум
Сообщений всего: 11
Дата рег-ции: Окт. 2009
Помог: 0 раз(а)
|
Работаю над одним стороннем скрипте, в нем есть обработчик шаблонов.
Вот его функция, которая непосредственно занимается обработкой шаблона.
PHP:
скопировать код в буфер обмена
/************************************************************************* * function CreatePageEcho () * Echos the finished template * USAGE example: * $page->CreatePageEcho(); * ***********************************************************************/ function CreatePageEcho ($lang,$config) { global $settings_tpl; $lang['BUYERU'] = $settings_tpl['buyers_title']; $lang['PROVIDERU'] = $settings_tpl['providers_title']; if(count($comment_matches['0']) != 0 ) { foreach($comment_matches['0'] as $key => $value) { } } foreach ($settings_tpl as $key => $value) $this->html = str_replace("{TPL.".$key."}", $value, $this->html); foreach ($lang as $key => $value) { $template_name = '{LANG_' . $key . '}'; $this->html = str_replace ($template_name, $value, $this->html); } foreach ($this->parameters as $key => $value) { $template_name = '{' . $key . '}'; $this->html = str_replace ($template_name, $value, $this->html); } $this->html = str_replace ('{SITE_URL}', $config['site_url'], $this->html); $this->html = str_replace ('{TPL_NAME}', $config['tpl_name'], $this->html); $this->html = str_replace ('{CURRENCY_SIGN}', $config['currency_sign'], $this->html); $this->html = str_replace ('{CURRENCY_CODE}', $config['currency_code'], $this->html); $this->html = str_replace ('{CURRENCY_POS}', $config['currency_pos'], $this->html); if(isset($_SESSION['user']['id'])) { $this->html = str_replace ('{LOGGED_IN}', '1', $this->html); } else { $this->html = str_replace ('{LOGGED_IN}', '0', $this->html); } if(isset($config['temp_php'])) { if($config['temp_php']) { } } /*$ifmatches = array(); while(preg_match('/IF\(\"(.*?)\"(.*?)\"(.*?)\"\)\{([^\{]*?)\{:IF\}/s', $this->html, $ifmatches)) { print_r($ifmatches);//foreach ($ifmatches['0'] as $key => $value) { if(trim($ifmatches['2']) == '!=') { if($ifmatches['1'] != $ifmatches['3']) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } elseif(trim($ifmatches['2']) == '==') { if($ifmatches['1'] == $ifmatches['3']) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } elseif(trim($ifmatches['2']) == '<') { if($ifmatches['1'] < $ifmatches['3']) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } elseif(trim($ifmatches['2']) == '>') { if($ifmatches['1'] > $ifmatches['3']) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } elseif(trim($ifmatches['2']) == '%') { $mod = $ifmatches['1']%$ifmatches['3']; if($mod == 0) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } } }*/ preg_match_all('/IF\(\"(.*?)\"(.*?)\"(.*?)\"\)\{(.*?)\{:IF\}/s', $this->html, $ifmatches); //while(preg_match_all('/IF\(\"(.*?)\"(.*?)\"(.*?)\"\)\{(.*?)\{:IF\}/s', $this->html, $ifmatches)) if(count($ifmatches['0']) != 0 ) { foreach ($ifmatches['0'] as $key => $value) { if(trim($ifmatches['2'][$key]) == '!=') { if($ifmatches['1'][$key] != $ifmatches['3'][$key]) { $this->html = str_replace($value, $ifmatches['4'][$key], $this->html); } else { } } elseif(trim($ifmatches['2'][$key]) == '==') { if($ifmatches['1'][$key] == $ifmatches['3'][$key]) { $this->html = str_replace($value, $ifmatches['4'][$key], $this->html); } else { } } elseif(trim($ifmatches['2'][$key]) == '<') { if($ifmatches['1'][$key] < $ifmatches['3'][$key]) { $this->html = str_replace($value, $ifmatches['4'][$key], $this->html); } else { } } elseif(trim($ifmatches['2'][$key]) == '>') { if($ifmatches['1'][$key] > $ifmatches['3'][$key]) { $this->html = str_replace($value, $ifmatches['4'][$key], $this->html); } else { } } elseif(trim($ifmatches['2'][$key]) == '%') { $mod = $ifmatches['1'][$key]%$ifmatches['3'][$key]; if($mod == 0) { $this->html = str_replace($value, $ifmatches['4'][$key], $this->html); } else { } } } } echo $this->html; }
Сам шаблон выглядит примерно так:
CODE ( html):
скопировать код в буфер обмена
{OVERALL_HEADER} <table width="780" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><br> <span class="title1">{LANG_PROJECTSU}</span><br /><br> <table width="100%" cellpadding="0" cellspacing="1" border="0" class="table_titles"> <tr bgcolor="#E1E1E1" align="center"> <td width="40%" align="left" height="18" style="padding-left:5px;">{LANG_PROJNAME}</td> <td width="6%" bgcolor="#E1E1E1">{LANG_BIDS}</td> <td width="30%" align="left" style="padding-left:5px;">{LANG_JOBTYPE}</td> <td width="12%">{LANG_STARTED}</td> <td width="12%">{LANG_ENDS}</td> </tr> {LOOP: PROJECTS} <tr align="center"> <td align="left" style="padding-left:5px;"><a href="{PROJECTS.link}">{PROJECTS.title}</a> IF("{PROJECTS.featured}"=="1"){ <span class="featured">{LANG_FEATURED}</span>{:IF} IF("{PROJECTS.urgent}"=="1"){ <span class="urgent">{LANG_URGENT}</span>{:IF}</td> <td>{PROJECTS.bids}</td> <td align="left" style="padding-left:5px;">{PROJECTS.types}</td> <td>{PROJECTS.startdate}</td> <td>{PROJECTS.enddate}</td> </tr> <tr> <Td colspan="5" background="templates/{TPL_NAME}/images/hline_dot.gif"><img src="templates/{TPL_NAME}/images/dot.gif" width="1" height="1" alt="" border="0"></TD> </tr> {/LOOP: PROJECTS} <tr> <td colspan="5"><br> <br></td> </tr> </table> </td> </tr> </table> {OVERALL_FOOTER}
Собственно, блок IF выглядит так:
Однако, он может быть только одного уровня. Как можно переделать эту функцию, чтобы можно было использовать несколько уровней?
А вот моя попытка:
PHP:
скопировать код в буфер обмена
while(preg_match('/IF\(\"(.*?)\"(.*?)\"(.*?)\"\)\{([^\{]*?)\{:IF\}/s', $this->html, $ifmatches)) { print_r($ifmatches);//foreach ($ifmatches['0'] as $key => $value) { if(trim($ifmatches['2']) == '!=') { if($ifmatches['1'] != $ifmatches['3']) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } elseif(trim($ifmatches['2']) == '==') { if($ifmatches['1'] == $ifmatches['3']) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } elseif(trim($ifmatches['2']) == '<') { if($ifmatches['1'] < $ifmatches['3']) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } elseif(trim($ifmatches['2']) == '>') { if($ifmatches['1'] > $ifmatches['3']) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } elseif(trim($ifmatches['2']) == '%') { $mod = $ifmatches['1']%$ifmatches['3']; if($mod == 0) { $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html); } else { $this->html = str_replace($ifmatches['0'], '', $this->html); } } } }
И не получилось.
Весь шаблонизатор во вложенном файле.
(Отредактировано автором: 06 Октября, 2011 - 06:45:20)
|