Sign in to follow this  

[web] Redesign of my Template Engine (PHP)

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm looking to draft a new version of my templating engine. I love the way mine currently works, but there's a major flaw in it. I'm trying to rework it, to remove this flaw. Here's the gist of my current approach: 1. All page requests are forwarded to index.php 2. Index.php parses the URI, gets a database connection, builds a base module, then hands the current page (the base template) to a specific module (articles module, let's say) with the database connection. 3. Article's module runs it's code, alters the anchor in the base template and returns back to index.php. 4. Index.php echos the page, shuts down the database. Here's an example:
<?php
     $szPage = "";
     $Database = new Database;

     require_once("module_base.php");
     $Base = new Base($Database);
     $Base->Run(&$szPage);

     // Assume it builds "Articles" out of grozzler.com/articles/
     $ParsedURI = ParseTheURI();

     require_once("module_". $ParsedURI .".php);
     $Module = new $ParsedURI($Database);
     $Module->Run(&$szPage);

     echo $szPage;
     $Database->Disconnect();
?>

It's a very clean system that works well. The fatal flaw is: It's a BITCH to change the base template. So if you're looking for something like eBay, where the index is different from the search which is different from the item page, it's nigh unto impossible to do. Now the system works, if you have a consistant page, and is really quite nice as well. The abstraction from content and logic is phenominal, and none of my PHP scripts are ever going to have HTML in them again (it's just that awesome). Here's what I'm going to be doing for the next rewrite: 1. The URI is parsed a module script is instantly invoked. (module rewrite from Apache will forward the URI requests) 2. Modules will depend on $szPage being set, to handle their page modification. 3. The base module will invoke child modules, to modify the page. You might have a login bar on the right on some pages, and not on others, so the Articles module will require_once("login_sidebar.php") which will modify $szPage and replace tne anchor in the template (assuming the articles page has the {LOGIN_SIDEBAR} anchor in the template). 4. The base module will be responsible for database connections and showing of the page, rather than handing it back to some index.php I think this system will work out well, because now any module can use any template for the base that it wants. A typical page will look something like this:
<?php
     $szPage = "";
     $ArticleTemplate = new Template("Article.tpl");
     $szPage = $ArticleTemplate->GetTemplate("Base");

     // Modifies the $szPage dependency.
     require_once("login_sidebar.php");

     // Assume that an article template is filled in here.

     echo $szPage;
?>

I do however, have a concern with this approach. The global dependency on $szPage being filled in. Obviously it's "just stupid" if a programmer doesn't declare $szPage and set it to a base template before invoking the child modules, but it's also "just stupid" if a C++ programmer sets a random pointer to NULL. I want to try to minimize these mistakes. Any ideas or suggestions on how to improve it?

Share this post


Link to post
Share on other sites
You seem pretty set on your system and I think it's a rather nice one at that. I personally enjoy simplicity and effectiveness. If it works, that's perfect.

A general page on my site has $title declared, optionally $menu and then $content. It then calls a function called displayPage() with those variables and the template name (optional) I want as the parameters. Right now I only have one template, but I'll definately experiment with more later on. It's nothing compared to the modular system you have, but I strive for simplicity and effectiveness.

I think the system you have worked out sounds very nice. Get working on it! I'm looking forward to the final product...

Also, change your menu system. It seems a bit dull compared to the rest of the site. Looks like Andrei's. but I'm sure it'll be awesomely easy to change the template once your new system is in tact [wink]. Oh, and just noticed the random header image. Nicely done.

Share this post


Link to post
Share on other sites
Quote:
Original post by GroZZleR
A typical page will look something like this:
*** Source Snippet Removed ***

I do however, have a concern with this approach. The global dependency on $szPage being filled in. Obviously it's "just stupid" if a programmer doesn't declare $szPage and set it to a base template before invoking the child modules, but it's also "just stupid" if a C++ programmer sets a random pointer to NULL. I want to try to minimize these mistakes.

Any ideas or suggestions on how to improve it?


Are you only concerned if $szPage hasn't been set, or hasn't been set to a proper template? I mean if there's something wrong with a template, the visual side is going to show, so that's not too much of a problem. However, if you're worried about the actual contents of $szPage, then why not have a check to see if it's been set?


if (!isset($szPage)
$Module->SetError("\$szPage undefined. Check $_SERVER['PHP_SELF'] for proper initialisation of \$szPage.");


In theory, if you're not happy with this and wanting to check that $szPage was valid after a call to GetTemplate(), then you'd probably have to carry out some kind of schema validation on the template file.

I really think checking the value of $szPage should be enough though. The other way is messy and would certain cause a performance hit.

Share this post


Link to post
Share on other sites

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this