Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/topic.php on line 737
Форумы портала PHP.SU :: Вложенные блоки IF в шаблонизаторе

 PHP.SU

Программирование на PHP, MySQL и другие веб-технологии
PHP.SU Портал     На главную страницу форума Главная     Помощь Помощь     Поиск Поиск     Поиск Яндекс Поиск Яндекс     Вакансии  Пользователи Пользователи


 Страниц (1): [1]   

> Без описания
Jason Bourne
Отправлено: 06 Октября, 2011 - 06:43:55
Post Id


Новичок


Покинул форум
Сообщений всего: 11
Дата рег-ции: Окт. 2009  


Помог: 0 раз(а)




Работаю над одним стороннем скрипте, в нем есть обработчик шаблонов.

Вот его функция, которая непосредственно занимается обработкой шаблона.
PHP:
скопировать код в буфер обмена
  1.  
  2. /*************************************************************************
  3.         * function CreatePageEcho ()
  4.         * Echos the finished template
  5.         * USAGE example:
  6.         * $page->CreatePageEcho();
  7.         * ***********************************************************************/
  8.         function CreatePageEcho ($lang,$config)
  9.         {
  10.                 global $settings_tpl;
  11.                
  12.                 $lang['BUYERU'] = $settings_tpl['buyers_title'];
  13.                 $lang['PROVIDERU'] = $settings_tpl['providers_title'];
  14.  
  15.         preg_match_all('/\{\:\:(.*?)\:\}/s',$this->html,$comment_matches);
  16.  
  17.         if(count($comment_matches['0']) != 0)
  18.         {
  19.           foreach($comment_matches['0'] as $key => $value)
  20.           {
  21.             $this->html = str_replace($value,'', $this->html);
  22.           }
  23.         }
  24.  
  25.                 foreach ($settings_tpl as $key => $value) $this->html = str_replace("{TPL.".$key."}", $value, $this->html);
  26.  
  27.                 foreach ($lang as $key => $value)
  28.                 {
  29.                         $template_name = '{LANG_' . $key . '}';
  30.                         $this->html = str_replace ($template_name, $value, $this->html);
  31.                 }      
  32.                 foreach ($this->parameters as $key => $value)
  33.                 {
  34.                         $template_name = '{' . $key . '}';
  35.  
  36.                         $this->html = str_replace ($template_name, $value, $this->html);
  37.                 }      
  38.                
  39.                 $this->html = str_replace ('{SITE_URL}', $config['site_url'], $this->html);
  40.                 $this->html = str_replace ('{TPL_NAME}', $config['tpl_name'], $this->html);
  41.                 $this->html = str_replace ('{CURRENCY_SIGN}', $config['currency_sign'], $this->html);
  42.                 $this->html = str_replace ('{CURRENCY_CODE}', $config['currency_code'], $this->html);
  43.                 $this->html = str_replace ('{CURRENCY_POS}', $config['currency_pos'], $this->html);
  44.                
  45.                 if(isset($_SESSION['user']['id']))
  46.                 {
  47.                         $this->html = str_replace ('{LOGGED_IN}', '1', $this->html);
  48.                 }
  49.                 else
  50.                 {
  51.                         $this->html = str_replace ('{LOGGED_IN}', '0', $this->html);
  52.                 }
  53.  
  54.                 if(isset($config['temp_php']))
  55.                 {
  56.                         if($config['temp_php'])
  57.                         {
  58.                                 $this->html = preg_replace_callback("/(<\?=)(.*?)\?>/si", array('HtmlTemplate', 'EvalPrintBuffer'),$this->html);
  59.                                 $this->html = preg_replace_callback("/(<\?php|<\?)(.*?)\?>/si", array('HtmlTemplate', 'EvalBuffer'),$this->html);
  60.                         }
  61.                 }
  62.  
  63.                 /*$ifmatches = array();
  64.         while(preg_match('/IF\(\"(.*?)\"(.*?)\"(.*?)\"\)\{([^\{]*?)\{:IF\}/s', $this->html, $ifmatches))
  65.                 {
  66.                         print_r($ifmatches);//foreach ($ifmatches['0'] as $key => $value)
  67.                         {
  68.                                 if(trim($ifmatches['2']) == '!=')
  69.                                 {
  70.                                         if($ifmatches['1'] != $ifmatches['3'])
  71.                                         {
  72.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  73.                                         }
  74.                                         else
  75.                                         {
  76.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  77.                                         }
  78.                                 }
  79.                                 elseif(trim($ifmatches['2']) == '==')
  80.                                 {
  81.                                         if($ifmatches['1'] == $ifmatches['3'])
  82.                                         {
  83.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  84.                                         }
  85.                                         else
  86.                                         {
  87.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  88.                                         }
  89.                                 }
  90.                                 elseif(trim($ifmatches['2']) == '<')
  91.                                 {
  92.                                         if($ifmatches['1'] < $ifmatches['3'])
  93.                                         {
  94.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  95.                                         }
  96.                                         else
  97.                                         {
  98.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  99.                                         }
  100.                                 }
  101.                                 elseif(trim($ifmatches['2']) == '>')
  102.                                 {
  103.                                         if($ifmatches['1'] > $ifmatches['3'])
  104.                                         {
  105.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  106.                                         }
  107.                                         else
  108.                                         {
  109.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  110.                                         }
  111.                                 }
  112.                                 elseif(trim($ifmatches['2']) == '%')
  113.                                 {
  114.                                         $mod = $ifmatches['1']%$ifmatches['3'];
  115.  
  116.                                         if($mod == 0)
  117.                                         {
  118.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  119.                                         }
  120.                                         else
  121.                                         {
  122.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  123.                                         }
  124.                                 }
  125.                         }
  126.                 }*/
  127.  
  128.                 $ifmatches = array();
  129.                 preg_match_all('/IF\(\"(.*?)\"(.*?)\"(.*?)\"\)\{(.*?)\{:IF\}/s', $this->html, $ifmatches);
  130.                 //while(preg_match_all('/IF\(\"(.*?)\"(.*?)\"(.*?)\"\)\{(.*?)\{:IF\}/s', $this->html, $ifmatches))
  131.                 if(count($ifmatches['0']) != 0)
  132.                 {
  133.                         foreach ($ifmatches['0'] as $key => $value)
  134.                         {
  135.                                 if(trim($ifmatches['2'][$key]) == '!=')
  136.                                 {
  137.                                         if($ifmatches['1'][$key] != $ifmatches['3'][$key])
  138.                                         {
  139.                                                 $this->html = str_replace($value, $ifmatches['4'][$key], $this->html);
  140.                                         }
  141.                                         else
  142.                                         {
  143.                                                 $this->html = str_replace($value, '', $this->html);
  144.                                         }
  145.                                 }
  146.                                 elseif(trim($ifmatches['2'][$key]) == '==')
  147.                                 {
  148.                                         if($ifmatches['1'][$key] == $ifmatches['3'][$key])
  149.                                         {
  150.                                                 $this->html = str_replace($value, $ifmatches['4'][$key], $this->html);
  151.                                         }
  152.                                         else
  153.                                         {
  154.                                                 $this->html = str_replace($value, '', $this->html);
  155.                                         }
  156.                                 }
  157.                                 elseif(trim($ifmatches['2'][$key]) == '<')
  158.                                 {
  159.                                         if($ifmatches['1'][$key] < $ifmatches['3'][$key])
  160.                                         {
  161.                                                 $this->html = str_replace($value, $ifmatches['4'][$key], $this->html);
  162.                                         }
  163.                                         else
  164.                                         {
  165.                                                 $this->html = str_replace($value, '', $this->html);
  166.                                         }
  167.                                 }
  168.                                 elseif(trim($ifmatches['2'][$key]) == '>')
  169.                                 {
  170.                                         if($ifmatches['1'][$key] > $ifmatches['3'][$key])
  171.                                         {
  172.                                                 $this->html = str_replace($value, $ifmatches['4'][$key], $this->html);
  173.                                         }
  174.                                         else
  175.                                         {
  176.                                                 $this->html = str_replace($value, '', $this->html);
  177.                                         }
  178.                                 }
  179.                                 elseif(trim($ifmatches['2'][$key]) == '%')
  180.                                 {
  181.                                         $mod = $ifmatches['1'][$key]%$ifmatches['3'][$key];
  182.  
  183.                                         if($mod == 0)
  184.                                         {
  185.                                                 $this->html = str_replace($value, $ifmatches['4'][$key], $this->html);
  186.                                         }
  187.                                         else
  188.                                         {
  189.                                                 $this->html = str_replace($value, '', $this->html);
  190.                                         }
  191.                                 }
  192.                         }
  193.                 }
  194.  
  195.                 echo $this->html;
  196.         }
  197.  


Сам шаблон выглядит примерно так:
CODE (html):
скопировать код в буфер обмена
  1.  
  2. {OVERALL_HEADER}
  3. <table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
  4.   <tr>
  5.     <td><br>      
  6.       <span class="title1">{LANG_PROJECTSU}</span><br /><br>
  7.       <table width="100%" cellpadding="0" cellspacing="1" border="0" class="table_titles">
  8.         <tr bgcolor="#E1E1E1" align="center">
  9.           <td width="40%" align="left" height="18" style="padding-left:5px;">{LANG_PROJNAME}</td>
  10.           <td width="6%" bgcolor="#E1E1E1">{LANG_BIDS}</td>
  11.           <td width="30%" align="left" style="padding-left:5px;">{LANG_JOBTYPE}</td>
  12.           <td width="12%">{LANG_STARTED}</td>
  13.           <td width="12%">{LANG_ENDS}</td>
  14.         </tr>
  15.   {LOOP: PROJECTS}
  16.   <tr align="center">
  17.     <td align="left" style="padding-left:5px;"><a href="{PROJECTS.link}">{PROJECTS.title}</a> IF("{PROJECTS.featured}"=="1"){&nbsp; <span class="featured">{LANG_FEATURED}</span>{:IF} IF("{PROJECTS.urgent}"=="1"){&nbsp; <span class="urgent">{LANG_URGENT}</span>{:IF}</td>
  18.     <td>{PROJECTS.bids}</td>
  19.     <td align="left" style="padding-left:5px;">{PROJECTS.types}</td>
  20.     <td>{PROJECTS.startdate}</td>
  21.     <td>{PROJECTS.enddate}</td>
  22.   </tr>
  23.   <tr>
  24.     <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>
  25.   </tr>
  26.   {/LOOP: PROJECTS}
  27.   <tr>
  28.     <td colspan="5"><br>
  29.       <br></td>
  30.   </tr>
  31.       </table>
  32.  </td>
  33.   </tr>
  34. </table>
  35. {OVERALL_FOOTER}
  36.  

Собственно, блок IF выглядит так:
CODE (html):
скопировать код в буфер обмена
  1. IF("{PROJECTS.featured}"=="1"){&nbsp; <span class="featured">{LANG_FEATURED}</span>{:IF}

Однако, он может быть только одного уровня. Как можно переделать эту функцию, чтобы можно было использовать несколько уровней?

А вот моя попытка:
PHP:
скопировать код в буфер обмена
  1. $ifmatches = array();
  2.         while(preg_match('/IF\(\"(.*?)\"(.*?)\"(.*?)\"\)\{([^\{]*?)\{:IF\}/s', $this->html, $ifmatches))
  3.                 {
  4.                         print_r($ifmatches);//foreach ($ifmatches['0'] as $key => $value)
  5.                         {
  6.                                 if(trim($ifmatches['2']) == '!=')
  7.                                 {
  8.                                         if($ifmatches['1'] != $ifmatches['3'])
  9.                                         {
  10.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  11.                                         }
  12.                                         else
  13.                                         {
  14.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  15.                                         }
  16.                                 }
  17.                                 elseif(trim($ifmatches['2']) == '==')
  18.                                 {
  19.                                         if($ifmatches['1'] == $ifmatches['3'])
  20.                                         {
  21.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  22.                                         }
  23.                                         else
  24.                                         {
  25.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  26.                                         }
  27.                                 }
  28.                                 elseif(trim($ifmatches['2']) == '<')
  29.                                 {
  30.                                         if($ifmatches['1'] < $ifmatches['3'])
  31.                                         {
  32.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  33.                                         }
  34.                                         else
  35.                                         {
  36.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  37.                                         }
  38.                                 }
  39.                                 elseif(trim($ifmatches['2']) == '>')
  40.                                 {
  41.                                         if($ifmatches['1'] > $ifmatches['3'])
  42.                                         {
  43.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  44.                                         }
  45.                                         else
  46.                                         {
  47.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  48.                                         }
  49.                                 }
  50.                                 elseif(trim($ifmatches['2']) == '%')
  51.                                 {
  52.                                         $mod = $ifmatches['1']%$ifmatches['3'];
  53.  
  54.                                         if($mod == 0)
  55.                                         {
  56.                                                 $this->html = str_replace($ifmatches['0'], $ifmatches['4'], $this->html);
  57.                                         }
  58.                                         else
  59.                                         {
  60.                                                 $this->html = str_replace($ifmatches['0'], '', $this->html);
  61.                                         }
  62.                                 }
  63.                         }
  64.                 }


И не получилось.

Весь шаблонизатор во вложенном файле.
Скачать файл: class.template_engine.php
Скачан раз: 47

(Отредактировано автором: 06 Октября, 2011 - 06:45:20)

 
 Top
Страниц (1): [1]
Сейчас эту тему просматривают: 0 (гостей: 0, зарегистрированных: 0)
« Напишите за меня, пожалуйста »


Все гости форума могут просматривать этот раздел.
Только зарегистрированные пользователи могут создавать новые темы в этом разделе.
Только зарегистрированные пользователи могут отвечать на сообщения в этом разделе.
 



Powered by PHP  Powered By MySQL  Powered by Nginx  Valid CSS  RSS

 
Powered by ExBB FM 1.0 RC1. InvisionExBB