Public feed #376721

Unnamed

public text v1 · immutable
Raw Edit
·published 2007-03-01 06:32 UTC
rendered paste body
# COMING SOON: Multi-dimensional arrays for looping HTML. Holy Fuck.

# Little template class to perform functions such
# as storing the variables and parsing the files.
class Template {

	var $files = array();
	var $blocks = array();

	# When called, this enters a specific .tpl file
	# into the files array set above.
	function tpl_file($filename_array) {
		reset($filename_array);
		while(list($id, $filename) = each($filename_array)) {
			$this->files[$id] = $filename;
		}
	}

	# Basically, this adds each variable block, as well
	# as the content that is going to be swapped in said
	# variable block. I may change this later, depending
	# on how I decide to handle looping HTML code in TPL
	# files. The variable blocks, set above, holds all
	# of this information.
	function tpl_block($block_array) {
		reset($block_array);
		while(list($id, $content) = each($block_array)) {
			$this->blocks[$id] = $content;
		}
	}

	# This parses a specific template file. The .tpl file
	# must be registered using the tpl_file() function
	# before this will work. Once it has been entered,
	# you can call parse_file('TPL_ID'). This function
	# will search the files variable above for the TPL key
	# and open the .tpl file associated with it, and put
	# it into a variable called $code. It then starts from
	# the first array entry of blocks var above, and
	# replaces any instance of that block with the content
	# associated with it. Whew.
	function parse_file($id) {
		$code = file_get_contents('../template/' . $this->files[$id]);
		reset($this->blocks);
		while(list($id, $content) = each($this->blocks)) {
			$code = str_replace($id, $content, $code);
		}
		echo $code;
	}

}

# Tie a variable name to the class
$template = new Template;

# Store this filename in the class variable $files using
# the tpl_file function.
$template->tpl_file(array('body' => 'test.tpl'));

# Store these blocks in the class variable $blocks using
# the tpl_block function.
$template->tpl_block(array('{TEST_VARIABLE}' => 'This is a test!', '{TEST_VARIABLE2}' => 'This is another test!'));

# Parse the template file assigned to the ID "body".
$template->parse_file('body');

?>