Hey guys and gals, I’m curious about something and I would first like to say that I’m sorry if this isn’t the correct forum to post in, but seeing as though it’s a PHP forum I thought I’d ask here.
I have used many CMS, forums, albums and other such scripts over the years and I’ve noticed (from memory) that the index.php does not contain any or very little HTML. Why is that?
I’ve used several tutorials and books over the years when I urge to learn web development but I’m yet to find anything that explains this method. My only assumption is that it has something to do with themes or a certain layout of the script?
I’d appreciate anybody shining some light on this for me.
Well I use WordPress as my CMS of choice and when I check out the index.php file, I find hardly any HTML markup in there as you said.
The reason for this is because PHP is such a powerful language, especially when the developers of WordPress write it!
Just a simple command like;
< ?php the_content(); ?>
Will grab all of the content in a page and display it on the screen. Obviously this function has more lines of code behind it that does all of the hard work and it’s outputted as a function in this tiny little line.
However, when you’re dealing with static websites, they contain little or no PHP at all as it is just raw HTML such as h1 tags, p tags, ul’s, li’s, div’s etc… and this is just hard-coded into the page.
So what is the purpose of using that statement? Where is the actual HTML used?
I understand ‘how’ this works, I just don’t understand why. Where could I learn more about using this type of method? Sorry for all the questions, it has just been something that has confuzzled me in the past!
Look up MVC — Model, View, Controller.
HTML is related to View. While it is a markup language, it is the end product of the server’s action on a request. Any project that surpasses a certain size will need to differentiate the process of acting on a request from displaying results to the user.
For Content Management Systems, the existence of templates alone demonstrates how this works. The CMS pulls together all of the information that the user could want, then the user can determine how to display this through their templates. They apply their own markup, and then insert content using pre-determined methods provided by the CMS.
Look out for php includes:
< ? php include('somefile.php'); ?>
This was a huge thing for me when I started to work in PHP over static HTML – the index page will more than likely contain includes to stitch together the whole page.
A good example is the document header, with your css link, meta tags, scripts etc. All you need to one PHP document with this in, then include it in your pages in place of the header itself.
My index pages are usually just a handful of includes – one for options (connection strings, common variables etc) document header, site body, main/sub menus, Google Analytics code, etc. These pages will have a mixture of PHP and HTML.
I have yet to move on to OOPHP, so take my post as being a way of doing things when working with procedral PHP.
As for the “why,” you might be interested in what I describe as [php-first](http://dynamicdrive.com/forums/entry.php?267). Even if you don’t use a full-on MVC pattern, you can avoid a lot of problems in your PHP (and write more efficient code) if you simply make sure that **output** (HTML, etc., anything that goes to the browser) is the *last* part of your script.
> You may have a certain style for < h1 >< /h1 > so in wordpress, usually they have something like this;
> < h1 >< ?php the_permalink(); ?>< /h1 >
> Therefore you can style your post links according to your stylesheet.
There is an example of MVC at work here, but it is not to do with the stylesheet.
The the_permalink() function is provided by the CMS in order to access information the Model understands to be the permanent URL to a post. In wordpress, this function is documented as returning the raw text of the URL that is already being accessed by the script.
The point of that is that if the URL structure changes (eg. the site moving towards “friendly URLs”), the template does not have to change to reflect this. That data has nothing to do with the View (how the markup is structured) and has everything to do with the Model (what the information is). In addition, the Model has no interest in how the markup looks (the View), so an enterprising author might change:
< h1 >< ?php the_permalink(); ?>< /h1 >
To instead read:
< h2 >< ?php the_permalink(); ?>< /h2 >
< h1 >< a href="” title=”< ?php the_title_attribute(); ?>” >< ?php the_title(); ?>< /a >< /h1 >
The the_permalink() method stays the same, but the markup (View) has been changed quite a bit.
The stylesheet is slightly separate as it isn’t really a part of the software. It acts on the output of the software — it plays no part in constructing that output. I would therefore not call it part of the View (or any of the software’s MVC). However, I don’t hold strongly to that opinion — I just find it makes things clearer in my own head to view it that way.
You must be logged in to reply to this topic.
*May or may not contain any actual "CSS" or "Tricks".