Return to Snippet

Revision: 10563
at January 4, 2009 13:43 by thermosilla


Updated Code
<?php

function apply_template($file,$vars=array()){
	
	$template = file_get_contents($file);
	
	foreach ($vars as $key => $var)
	{
		$template = str_replace("[+$key+]", $var, $template);
	}
	
	return $template;
}

$template_vars = array(
	'title' => 'Hello World!',
	'text' => 'Hello!',
	'link' => 'http://www.bigsmoke.us/php-templates/functions'
);

$html = apply_template("_main.tpl",$template_vars);
echo $html;

#_main.tpl
# 
#
#<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
#	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
#<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr">
#<head>
#	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
#	<title>[+title+]</title>
#	
#</head>
#<body>
#	<h1>[+title+]</h1>
#	<p>[+text+]</p>
#	<a href="[+link+]">[+link+]</a>
#</body>
#</html>
?>

Revision: 10562
at January 4, 2009 13:29 by thermosilla


Initial Code
<?php

function apply_template($file,$vars=array(),$include_globals=false){
	
	$template = file_get_contents($file);
	
	foreach ($vars as $key => $var)
	{
		$template = str_replace("[+$key+]", $var, $template);
	}
	
	return $template;
}

$template_vars = array(
	'title' => 'Hello World!',
	'text' => 'Hello!',
	'link' => 'http://www.bigsmoke.us/php-templates/functions'
);

$html = apply_template("_main.tpl",$template_vars);
echo $html;

#_main.tpl
# 
#
#<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
#	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
#<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr">
#<head>
#	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
#	<title>[+title+]</title>
#	
#</head>
#<body>
#	<h1>[+title+]</h1>
#	<p>[+text+]</p>
#	<a href="[+link+]">[+link+]</a>
#</body>
#</html>
?>

Initial URL

                                

Initial Description
Based on Jdub7's Pure PHP Template function 

http://snipplr.com/view/10797/pure-php-template-function/

Initial Title
PHP Template Function

Initial Tags
php

Initial Language
PHP