Jump to content

Logically Designing an HTML/CSS template with PHP


pianoman993

Recommended Posts

I'm struggling to find the correct approach to creating a php website (obviously presented through HTML and CSS). I'm aware the include() and require() functions are essential in the process of creating a template. I have already tried creating a site using a switch statement to recognize webpages by sending variables via the _GET method in the url, however this did not seem logical since every page ended with an unnecessarily long url (ex: www.mydomain.com?p=login). So this all boils down to my originial question, what is the correct way to script a php template that does not force the programmer to write html for each page (ex. home.php, login.php, content.php). :/

- Mark

Link to comment
Share on other sites

1, getting your URL's sorted is a case of using mod_rewrite assuming you're on Apache. This will allow you to have things like mydomain.com/login instead of mydomain.com?p=login. Essentially it involves routing all your pages through the same page, as with the $_GET method, but having URL's that look like you're doing otherwise.

 

2, using a lightweight templating system might help you out a bit. As always, this article details some of the nuts and bolts, as well as provides fully working code - which I actually used to base my own template class on.

 

3, It might also be worth looking through a few simple MVC frameworks - notably CakePHP or CodeIgniter (there are loads of others, but I find these two quite simple to understand out of the box). Follow the tutorials for them and hopefully you'll see a nice way of structuring your code other than the traditional approach, and one that makes it incredibly difficult to keep on top of future development, upgrades, etc. Most frameworks use mod_rewrite (as mentioned in my first point) and some form of template engine (as mentioned in my second) so a nice decent popular framework should have you covered on all points.

 

Cheers

Link to comment
Share on other sites

Alrighty, well I have researched mod_rewrite, the template engine Smarty, and CodeIgniter.  I have learned a lot and am ready to make sense out of your response to my earlier post.

 

Question One: In my earlier post I had the desire to create a website in which I would have a logical and commonly used system of displaying and editing information. So I researched template engines. It simply seemed to me that template engines act as code repositories like PEAR and allow for separation of presentation and code. If this is the case, I wondered if this is the fundamental way sites such as Facebook and DeviantArt work.

 

Question Two: Also, I checked out mod_rewriting which was extremely informative describing how sites like Apple.com displayed their information. I brainstormed a couple of ideas on how to quickly and painlessly integrate mod_rewriting with my own php "template" and this is the idea I thought up: You have one main .php file that checks the url against the basename() function for the name of the current script. With the name of the current script (such as index.php) a switch statement detects the filename and inserts the correlating module. Thus in this way you could have one file such as config.php included in every file on your page. In otherwords contact.php would have a config.php included inside of it that would indentify the file presently viewed as contact.php and load the proper module. Is this a sensible solution?

 

Two questions, any help would really very very useful.

 

Thanks,

Mark

Link to comment
Share on other sites

personally i reckon you should install a common PHP framework or application (CodeIgniter, CakePHP, Wordpress, SMF, etc) and see how they do it. Most of your questions would be easier understood by actually looking at the answers in practice and working things out from there.

 

As for templating engines - my favourite tutorial should shed some light on things. They don't all have to be full of custom tags, parsers, compilers etc - the whole point is just to seperate out HOW your site looks from how it works:

 

http://massassi.com/php/articles/template_engines/

Link to comment
Share on other sites

Thanks a bunch for sticking with my nooby questions, I hope I can just bug you a tad longer, hopefully for the final time. :)

 

I have installed a CodeIgniter Framework and have a rudimentary understanding of how it's new syntax works. Above you said that I should use the template engine Smarty. I downloaded it and tried to incorporate it into my website however, it seems that it has it's own syntax and operates the same way CodeIgniter does in seperating logic and presentation. Additionaly, I find it increasingly frustrating by the lack of websites explaining these special syntaxs employed by these frameworks. It has seemed through the various php blog pages I've visisted that Smarty and CodeIgniter are joyously simple and take little-to-no time to use.... argh! I hope I am just missing some really helpful websites with tutorials and explanations for beginners like me.

 

Thanks for everything,

Mark

Link to comment
Share on other sites

...Above you said that I should use the template engine Smarty...

 

lol trust me on this one - of all the recommendations I'd make, Smarty wouldn't be one of them ;) (pretty much for the same reasons you outline). Take a look at my first post above, under point number 2. There's a link in there which provides details of something that has the same concept as Smarty but just using PHP syntax to achieve it - and as a result, being much much faster.

Template engines are more concerned with seperating out the view from your application, where as MVC and its frameworks take things a step further by breaking up your application into two further parts - generally speaking, your data vs the business logic.

Try installing CakePHP or Zend Framework, which don't concern themselves with a whole new syntax and should hopefully make things a little clearer - else, play around with the code in the link I sent you - eventually it will click.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.