All pastes #552316 Raw Edit

Syosoft

public php v1 · immutable
·published 2007-06-09 07:05 UTC
rendered paste body
<!-- 	Description:	--------------------------------------------------------------------------------------	This file illustrates a simple strategy to get a php driven template system running.	Notice the start and stop of each file definition. This script assumes all 3 files are	in the same directory - probably not best practice, but best for illustration.	Code has not been tested - expect a PHP syntax error or two.--><!-- BEGIN TEMPLATE FILE: tpl.main.php --><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">	<head>		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />		<title>%title%</title>		<meta name="description" content="%description%" />		<meta name="keywords" content="%keywords%" />	</head>	<body>		%content%	</body></html><!-- END TEMPLATE FILE: tpl.main.php --><!-- BEGIN CONTENT FILE: home.php --><?php	set_tpl_var('title','Home Page');	set_tpl_var('description','This is our home page.');	set_tpl_var('keywords','These are key words');?><h1>Home Page</h1><p>This is the home page.</p><!-- END CONTENT FILE: home.php --><!-- BEGIN CONTROL FILE: index.php --><?php	$template_variables = array();	function set_tpl_var($name,val) { 		global $template_variables;		$template_variables[$name] = $val;	}	// Load template file	ob_start();	include 'tpl.main.php';	$template = ob_get_clean();	// Load content file	ob_start();	include 'home.php';	$content = ob_get_clean();	// Merge content into template	$output = str_replace('%content%',$content,$template);	// Find and replace all template variables	if(preg_match_all('/%(.*?)%',$output,$tpl_vars)) : 		foreach($tpl_vars[1] as $tpl_var) : 			$find 		= '%'.$tpl_var.'%';			$replace	= isset($template_variables[$tpl_var]) ? $template_variables[$tpl_var] : '';			$output		= str_replace($find,$replace,$output);		endforeach;	endif;	// Output page	echo $output;?><!-- END CONTROL FILE: index.php -->