Separating Code Logic From Display Logic
Jim
If you’re writing ASP.NET code in Visual Studio, then chances are you’re using “code-behind” modules. When using a code-behind, your code is almost completely separated from your display logic. In some instances, you need to make a logical decision at render-time, but generally, the only code you have in your HTML is HTML and CSS. When you build your ASP.NET project, you get a DLL and a matching HTML (.aspx) file. In ASP.NET 1.x, the HTML file was always going to be human-readable and looked the same before and after the build. In ASP.NET 2.0 (assumed in 3.0 as well) you can configure your build to create an HTML (.aspx) file which is only a placeholder for the browser. All of the display logic is also compiled into the DLL. If you ask me, I don’t like it, but it does help when you’re selling your code. Compiling everything into a DLL hides the original ASPX source code.
In PHP, you need to be a bit more dilligent with removing code logic from display logic. There are more than a couple handfuls of good templating engines out there, which allow you to “interpolate” variable values in your HTML code, and keep your PHP code completely separate. I’m partial to XTemplates myself, because they’re simple to use. For each page the user sees, I’ll create two files (not including a default header.php and footer.php): MyPage.php and MyPage.tpl (.tpl tells me it s a template file). I’ll generally keep the tpl files in a <current dir>/templates directory, and leave the PHP where it should be. The .tpl file contains nothing but HTML and the template specific notations.
The version of XTemplates that I use is probably old, but I love how it works, and it works well, so I’m not changing. I’m sure the concept hasn’t changed much, even if everything else has. Here’s how XTemplates works:
[PHP]
<?php
//assumed inclusion of the xtemplate library
$tpl = new XTemplate(’thetemplate.tpl’);
for($i = 0; $i < 10; $i++){
$tpl->assign(’theNumber’, $i);
$tpl->parse(’main.listItem’);
}
$tpl->parse(’main’);
$tpl->out(’main’);
?>
[HTML]
<!– BEGIN: main –>
<html>
<body>
<ul>
<!– BEGIN: listItem –>
<li>{theNumber}</li>
<!– END: listItem –>
</ul>
</body>
</html>
<!– END: main –>
[Output]
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
It’s simple. The PHP code runs a for loop. Each time through, it assigns $i to the “theNumber” variable in the template. It then “parses” the <li></li> block (main.listItem) and repeats. After the for loop, we “parse” the “main” block -> which contains all of the main.listItem generated text). We’ve now got all of our HTML text in the output buffer, so we send it to the browser with $tpl->out(”main”); That’s not too difficult.
This example shows you how clean your HTML and PHP remain when separated from one another. It also gives the page production team the ability to write HTML without having to manipulate PHP code, and gives the coder freedom to write code without being bothered with HTML (although they should know HTML - but that’s another post).
I’ve only touched on PHP and ASP.NET here, but I know your platform of choice offers some method for splitting code and display logic. Using this style of development will improve readability, writeability, performance, and development time.
Posted in Web Programming |