Remember?
Register
Back

Templating

By Monkeymatt last edited on Saturday, May 20, 2006 at 1:11:02 pm by Monkeymatt. Page 1.

123456789Next >>

As you read this, you are probably wondering why you would need a templating class when you can just throw HTML all over in the PHP code, potentially making it easier and faster to code. This is not the case when you come to larger projects. Combining the layout code and the processing code in one file makes gigantic files that are hard to edit and fix problems. Also, having the templates in seperate files allows you to change the look of your site easily or add other dynamic templates with the exact information on them very easily. All of this requires a templating system.

Before you can even figure out how to make this templating engine, you need to figure out what the template input files will look like. In this tutorial we will be using the following convention:

Variables to be replaced:
Code
{dynamic_variable}


Loops or sections of code, which can also have variables in them:
Code
<{loop}>This is the {num} time through the loop<{/loop}>


All of this can be combined into a valid HTML file like the one below that will eventually have dynamic content in it:
Code

<html>
<head>
<title>Test Page for Template</title>
<style type="text/css">
table{
border: solid 2px #000000;
}
</style>
</head>
<body>
{title}
<{lotsopeople}>
<table>
<tr><th colspan='2'>{maker}</th></tr>
<tr><th>Name</th><th>Note</th></tr>
<{notebook}>
<tr><td>{name}</td><td>{note}</td></tr>
<{/notebook}>
</table>
<div> </div>
<{/lotsopeople}>
<{anotherloop}>
<div>We Love Loops</div>
<{/anotherloop}>
</body>
</html>


On the next page, we will begin figuring out how we can make this simple HTML page come to life.

123456789Next >>