Jump to content

Creating a Template Engine


Kevin M

Recommended Posts

I'm interested in using a template engine for my current project. There's only 1 thing I want it to do, and that's let me keep the main structural HTML to one file, rather than having it in others. I don't want to be able to have special loops and all that stuff, just the HTML, and have the content changed depening on the page.There's a few different folders which will have the PHP files (just content and PHP code) in them. I'm going to use the $_GET array for the URL set up. Here's what I have as an idea:

index.php?game&page=bank
To explain (index.php is the main file). The red part is the first $_GET array, and the blue is the second one. I'm going to write a script that will dynamically generate the $_GET array. So this URL will tell the script to grab the bank.php page from the folder game. (so the link to bank.php would be /game/bank.php). I haven't quite figured out this script yet, but that's not important yet.What I'm confused on is writing the actual template engine. I know that I will need some basic functions, one to replace the tags (I'll probably do something like {content} or something like that), one to display, one to set (eg. $tpl->set("title", $title) [from bTemplate])/ assign something to some data, and maybe a few other ones.So I'm thinking, using file_get_contents() to grab the contents from the file with the content, then use str_replace to replace the {content} with the contents from the file. Would that work? So here's a quick code I drew up:
<?phpinclude('template.php'); //the PHP Template code$tpl = new template();$title = "Hello!";$tpl->assign("title", $title);$file = file_get_contents(page.php);$tpl->assign("content", $file);$template = "template.html";$final = $tpl->replace($template,$file);echo $final;?>

Or something similar to that. That's what I've got in my mind currently. So does that seem like a good way to go about doing it? I can't seem to find a better one, but any suggestions are welcome.Thanks,Kevin

Link to comment
Share on other sites

First, a template is a file with mostly HTML code, plus some extra template tags. The template usually has no PHP or executable code in it. One of my templates for a page header looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>  <head>	<title>{*page_title}</title>	<link rel="stylesheet" type="text/css" href="include/style.css" />	<meta http-equiv="imagetoolbar" content="no" />	<script type="text/javascript">	function toggle_vis(elId)	{	  el = document.getElementById(elId);	  el.style.display = (el.style.display == "none" ? "block" : "none");	}	</script>  </head>  <body>	<div id="content">	  <div id="header"> </div>	  <div id="submenu">{*type=if_start name=gallery_submenu}		<a id="submenu_digital" href="gallery.php?id={*id_digital}">digital</a><br />		<a id="submenu_traditional" href="gallery.php?id={*id_traditional}">traditional</a><br />		<a id="submenu_sketch" href="gallery.php?id={*id_sketchbook}"></a>{*type=if_end}	  </div>

Mostly it is HTML, but it has some extra stuff like this: <title>{*page_title}</title>or this:{*type=if_start name=gallery_submenu}Those are the template tags. The first one just gets replaced with a variable called page_title, and the second one is an if statement that shows some HTML if the gallery_submenu variable is set. There can be quite a few different tags.The template engine is usually a class that you create in PHP where you have PHP send all the variables to the template engine, tell it which template to load, and the template engine parses all the template tags and creates the HTML output.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...