NameSilo

[Resolved] [Php] Templating system

Spaceship Spaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
PHP:
 templating system[/b]

Basically i am creating a templating system where the templates are html files with some variables in them like "$username" etc etc.

I am wondering in the class's function that actually brings the template in i will have to global every single variable that the templates could possible use, which seems stupid, is there any other way around it?

The only way around it that i can think of is instead of replacing the "$username" directly, have a string replace before the template is loaded changing say "{username}" in a template to what "$username" is, but is that not a huge load of resources?

For reference current class implementation (which currently won't work properly as i will have to global anything templates may use, argh!).
[php]
class templating
{
	// where the template files are kept (e.g ./templates/utopia)
	var $path;
	
	function set_path($path_wanted)
	{
		// first we need to check the template is actually there
		if($this->template_exists($path_wanted) == true)
		{
			$this->path = $path_wanted;
		}
		
		// else check to make sure utopia the defualt template is there so we can revert to that
		else
		{
			if($this->template_exists(PXB_ROOT.'templates/utopia/') == true)
			{
				$this->path = PXB_ROOT.'templates/utopia';
			}
			
			else
			{
				error_message('No template was available for use, check template folder exists if not please re-upload.');
			}
		}
	}
	
	function template_exists($template)
	{
		if (is_dir($template.'/') == true)
		{
			return true;
		}
		
		else
		{
			return false;
		}
	}
	
	function template_file_exists($template_file)
	{
		if (file_exists("{$this->path}/{$template_file}.html"))
		{
			return true;
		}
		
		else
		{
			return false;
		}
	}
	
	function load_template($file, $block='')
	{			
		$actual_file = $this->path . '/' . $file . '.html';
		
		// okay now check the template file is there and bring it in
		if($this->template_file_exists($file) == true)
		{
			if (empty($block))
			{
				$t = file_get_contents($actual_file);
				echo eval("<<<TMP\r\n".$t."\r\nTMP;\r\n");
			}
			
			else
			{
				$t = file_get_contents($actual_file);
	
				$pattern = '#\[block:\s*(' . $block . ')\](.*)\[/block:\s*(\1)\]#is';
	
				// find the wanted part of the template
				preg_match($pattern, $t, $matches);
	
				// return the contents to be evaled and echod out
				echo eval("<<<TMP\r\n".$matches[2]."\r\nTMP;\r\n");
			}
		}
		
		else
		{
			error_message("template {$file}.php file not available<br />
			{$this->path}");
		}
	}
}
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
I did my own template system once with {username} kind of variables replaced in html template by regex replace function and it was not huge resource spending. Basically it would be ok for most regular size pages, IMHO.
During optimization of my template system I use page about 1mg in size and it take about 1 second to parse it. So, my point is regular db requests could take more time than 10kb page to parse.
Plus, you can use some kind of caching on server-side and parse page only after update. I don't know you can apply this in your case, but I did it in my system.

IGV
 
0
•••
I reckon you're over complicating things. Just include a php file that has the top part of the template with all the variables echoed, and declare the variables before you include it. And the same for the bottom part of template..
 
1
•••
Just_Dave said:
I reckon you're over complicating things. Just include a php file that has the top part of the template with all the variables echoed, and declare the variables before you include it. And the same for the bottom part of template..

That is not templating, the whole point is to seperate it. Anyway i resolved this ages ago.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back