imho.ws
IMHO.WS  

Вернуться   IMHO.WS > Веб-мастеру > Веб-программирование
Опции темы
Старый 10.06.2004, 23:52     # 21
Sheryld
Full Member
 
Регистрация: 29.05.2002
Сообщения: 544

Sheryld Луч света в тёмном царствеSheryld Луч света в тёмном царствеSheryld Луч света в тёмном царствеSheryld Луч света в тёмном царствеSheryld Луч света в тёмном царстве
шаблоны.

вот мой элементарный класс работы с шаблонами на php(точнее каркас, который потом можно дополнить всем необходимым):

Код:
<?
	class CTemplate
	{
		var $arrTemplates = array(); // array for template files
		
		var $strParsedTemplate = "";
		
		var $strTemplateRootPath = "templates";
		var $strExt = "txt";
		var $strTagLimiter = "%";
		
		var $strTemplateName = "";
		
		var $arrTags = array(); // arry for tags
		
		function voidLoadTemplate($strFileName)
		{
			//clear varibles(this funcion for single template)
			unset($this->arrTemplates);
			unset($this->strTemplateName);
			
			
			$arrMatches = array();
			$strFilePath = $this->strTemplateRootPath . "/" . $strFileName . "." . $this->strExt;
			
			
			if (!file_exists($strFilePath))
			{
				echo "Error. File: <B>" . $strFilePath . "</B> not found\r\n";
				exit();
			}
			
			$this->strTemplateName = $strFileName; 
			
			$this->arrTemplates[$this->strTemplateName]['CONTENT'] = $this->strGetContent($strFilePath);
			$this->arrTemplates[$this->strTemplateName]['PARSED'] = false;
			
			
			//preg_match("/\w+\/(\w+\.$this->strExt)/i",$strFilePath,$arrMatches);
			
			//$strTemplateName = substr($arrMatches[1],0,(strlen($arrMatches[1])-(strlen($this->strExt)+1)));
			
			/*echo "<PRE>\r\n";
			print_r($this->arrTemplates);
			echo "</PRE>\r\n";
			*/
			
					
		}
		
		function voidLoadTags()
		{
			$arrTagsMatchesTemp = array();
			$arrTagsMatches = array();
			
			if (!empty($this->arrTemplates[$this->strTemplateName]['CONTENT']))
			{
				preg_match_all("/$this->strTagLimiter(\w+)$this->strTagLimiter/i",
				$this->arrTemplates[$this->strTemplateName]['CONTENT'],
				$arrTagsMatchesTemp);
				
				
				if (!empty($arrTagsMatchesTemp))
				{
					foreach( $arrTagsMatchesTemp[1] as $TagName)
					{
						$arrTagsMatches[$TagName] = "";
					}
					
					$this->arrTemplates[$this->strTemplateName]['TAGS'] = $arrTagsMatches;
					
				}
				
				/*$this->arrTemplates[$this->strTemplateName]['TAGS']['TITLExxxx'] = 'mytitle';
				
				echo "<PRE>\r\n";
				print_r($this->arrTemplates[$this->strTemplateName]['TAGS']);
				echo "</PRE>\r\n";*/
			}
			else 
			{
				echo "Error. File: <B>" . $this->strTemplateName . "</B> is empty.\r\n";
				exit();	
			}
			
			unset($arrTagsMatchesTemp);
			unset($arrTagsMatches);
		}
		
		function strGetContent($strFilePath)
		{
			return implode("",file($strFilePath));
		}
		
		function boolParseTemplate()
		{
			$strTagName = "";
			unset($this->strParsedTemplate);
			
			/*if ($this->arrTemplates[$this->strTemplateName]['PARSED'])
			{
				return true;
			}*/
			
			if (!empty($this->arrTemplates[$this->strTemplateName]['CONTENT']) &&
			!empty($this->arrTemplates[$this->strTemplateName]['TAGS']))
			{
				
				$this->strParsedTemplate = $this->arrTemplates[$this->strTemplateName]['CONTENT'];
				
				foreach ($this->arrTemplates[$this->strTemplateName]['TAGS'] as $TagName => $TagValue)
				{
					//$strTagName =  $this->strTagLimiter . $TagName . $this->strTagLimiter;
					
					$this->strParsedTemplate = str_replace($this->strTagLimiter . $TagName . $this->strTagLimiter,$TagValue,$this->strParsedTemplate);
					
					/*echo "<PRE>\r\n";
					print_r($this->arrTemplates[$this->strTemplateName]['CONTENT']);
					echo "</PRE>\r\n";*/
					
					//echo $this->strParsedTemplate;
				}
				
				$this->arrTemplates[$this->strTemplateName]['PARSED'] = true;
				return true;				
					
			}
			else
			{
				echo "Error. File: <B>" . $this->strTemplateName . "</B> is empty.\r\n";
				exit(0);
			}
			
			return false;
			
		}
		function voidViewTemplate()
		{
			if ($this->arrTemplates[$this->strTemplateName]['PARSED'])
			{
				echo $this->strParsedTemplate;
			} 
		}
		
	}

?>

примеры использования:

Код:
$tempObj = new CTemplate(); 
    
       
   $tempObj->voidLoadTemplate("eshe"); 
    
   $tempObj->voidLoadTags(); 
    
   $tempObj->arrTemplates['eshe']['TAGS']['TITLE'] = "My Title test"; 
   $tempObj->arrTemplates['eshe']['TAGS']['CONTENT'] = "My Content test"; 
    
    
   $tempObj->boolParseTemplate(); 
    
   $tempObj->voidViewTemplate(); 
    
   $tempObj->arrTemplates['eshe']['TAGS']['TITLE'] = "My Title test111"; 
   $tempObj->arrTemplates['eshe']['TAGS']['CONTENT'] = "My Content test111"; 
    
   $tempObj->boolParseTemplate(); 
    
   $tempObj->voidViewTemplate();
сами шаблоны выглядят так:

start_template===>

<TITLE>
%TITLE%
</TITLE>
<DIV>
%CONTENT%
</DIV>

<====end_template

т.е. по сути это обычный HTML со спец. тегами.

Последний раз редактировалось Sheryld; 10.06.2004 в 23:57.
Sheryld вне форума  
Старый 10.06.2004, 23:54     # 22
Hubbitus
мод
IMHO Кодер-200(6,7,8)
 
Регистрация: 29.03.2003
Адрес: Saint-Petersburg, Russia
Пол: Male
Сообщения: 2 734

Hubbitus Бог с наворотамиHubbitus Бог с наворотами
Hubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотамиHubbitus Бог с наворотами
Ну кавычки, там где их у меня небыло, в принципе и не обязательны... стиля там никакого в файле, для скорости проставить можно конечно...

Эта суть реализовывается на SSI (лучше с использованием MySQL) в пяток строк!
Если надо, могу написать и/или помочь в написании.
__________________
Я делаю Линукс! Присоединяйтесь к свободным людям!

Связаться со мной всегда можно по джабберу: Hubbitus@jabber.ru
Pahan-Hubbitus.
Hubbitus вне форума  

Опции темы

Ваши права в разделе
Вы НЕ можете создавать новые темы
Вы не можете отвечать в темах.
Вы НЕ можете прикреплять вложения
Вы НЕ можете редактировать свои сообщения

BB код Вкл.
Смайлы Вкл.
[IMG] код Выкл.
HTML код Выкл.

Быстрый переход


Часовой пояс GMT +4, время: 01:38.




Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.