---
title: "template"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/168690-template"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "Fuel"
published: "2008-02-03T14:45:20.000Z"
updated: "2008-02-03T16:52:13.000Z"
replies: 2
views: 425
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/168690-template"
---

# template

## #1 — Fuel, 2008-02-03T14:45Z

Kan detta göras annorlunda/bättre?

```php
class template_page
{

	function template_page($template)
	{
		$mmm = "D:/wamp/www/test/x/themes/default/" . $template . ".html";
		
		if (file_exists($mmm))
		{
			$this->template_page = join("", file($mmm));
		}
		else
		{
			die("Template file $template not found.");
		}
	}
	
	function parse_template($file)
	{
		ob_start();
		include($file);
		$buffer = ob_get_contents();
		ob_end_clean();
		return $buffer;
	}
	
	function replace_tags($tags = array())
	{
		if (sizeof($tags) > 0)
		{
			foreach ($tags as $tag => $data)
			{
		    	$data = (file_exists($data)) ? $this->parse_template($data) : $data;
		   		$this->template_page = eregi_replace("%" . $tag . "%", $data,
		    	$this->template_page);
			}
		}
		else
		{
		  die("No tags can be replaced.");
		}
	}
	
	function output()
	{
		echo $this->template_page;
	}
}
```

Permalänk: https://www.webforum.nu/p/168690

## #2 — jme, 2008-02-03T14:57Z

Iaf en sak: hårdkoda inte sökvägarna.

Sedan skulle jag hellre returnera datan i funktionen output() än att skriva ut den. 

Jag skulle försöka använda preg\_\*-baserade regex-funktioner då de skall vara snabbare än ereg\_\*-baserade.

Om du kör med PHP5 så skulle jag använda exceptions istället för die().

Permalänk: https://www.webforum.nu/p/2077283

## #3 — Fuel, 2008-02-03T16:52Z

sökvägen var bara så när jag testade

```php
class template_page
{

	function template_page($template)
	{
		$mmm = BASE_ROOT . "/" . TEMPLATEPATH . "/" . $template . ".html";
		
		if (file_exists($mmm))
		{
			$this->template_page = join("", file($mmm));
		}
		else
		{
			throw new Exception("Template file $template not found..");
		}
	}
	
	function parse_template($file)
	{
		ob_start();
		include($file);
		$buffer = ob_get_contents();
		ob_end_clean();
		return $buffer;
	}
	
	function replace_tags($tags = array())
	{
		if (sizeof($tags) > 0)
		{
			foreach ($tags as $tag => $data)
			{
		    	$data = (file_exists($data)) ? $this->parse_template($data) : $data;
		   		$this->template_page = eregi_replace("%" . $tag . "%", $data,
		    	$this->template_page);
			}
		}
		else
		{
			throw new Exception("No tags can be replaced.");
		}
	}
	
	function output()
	{
		return $this->template_page;
	}
}
```

 och..

```php
require('./load.php');
try
{
	$page = new template_page("test");
	
	$page->replace_tags(array(
		"title" => "TESTING", 
		"description" => "Welcome to my test", 
	));
	
	print $page->output();
}

catch (Exception $e)
{
    echo "MSG: ",  $e->getMessage(), "\n";
}
```

Värt att notera är att jag under mina 7år som hobby-php-kodare aldrig har använt classer förrän nu (senaste månaderna), därför kan det se lite förvirrat ut  :l

Permalänk: https://www.webforum.nu/p/2077306

---

Tråden på webben: https://www.webforum.nu/amne/php/168690-template
