Page 1 of 1

How do you template your site?

Posted: Thu Sep 10, 2009 12:09 am
by JellyFish
I was curious to know how the DevNeters design their php templates. What I mean by templates and templating in this case is how you re-use your code and markup. For example let's say you have a standard header area for your site. You could either copy and paste that header to the body of every page you create, or you could place the header within a separate file and include it on every page. The former solution is good if you want to be able to customize the header specifically for every page. The latter solution is much better because if you want to make global changes throughout your site you just change the header file, plus you can also make the header file dynamic where you set an variable to an array of different settings and the header file then acts according the $headerArgs array, thus making the header file dynamic enough to make specific changes for each page.

So I'm wondering how have you templated the sites you've built sites. My solution is to create a /blocks/ directory that contains an assortment of files like header.php and foother.php. Also it could contain other blocks such as php code blocks for session handling or whatever you wish. The only problem I'm having with this way of doing things is I don't know if I would want to make the header file a block that goes into the top of the body of every html page, or if I want to make it the top of every page (that is, include the doctype, head tag, etc.). Or maybe I could make another file for php at the top of every page called "preout.php" which basically means php code that is executed before any output is sent back to the browser.

I also thought I'd post this just to through what I'm doing out there for constructive criticism. Please leave a comment or add a suggestion.

Re: How do you template your site?

Posted: Thu Sep 10, 2009 12:54 am
by requinix
Personally, I went with a framework approach. Each file has a layout defined, each layout defines a template, and each file gets rendered into a specific part of that template.

But that takes work. Most templates can be split into "HTML that comes before the main content", "the content", and "HTML that comes after the main content". An include() at the top and bottom of each PHP page is one of the least harmful ways of doing it.

Code: Select all

<?php // a regular php file
 
$title = "Title of this page";
include $_SERVER["DOCUMENT_ROOT"] . "/blocks/header.php";
 
// page content
 
include $_SERVER["DOCUMENT_ROOT"] . "/blocks/footer.php";
?>
 
 
<?php // blocks/header.php
?>
<html>
<head>
<title><?php echo $title; ?>
</head>
<body>
 
 
<?php // blocks/footer.php
?>
</body>
</html>

Re: How do you template your site?

Posted: Thu Sep 10, 2009 1:20 am
by JellyFish
Okay so other people are doing this approach, and it's not so unconventional of me to do so as well?

Also I was thinking of defining an array for the header file.

Code: Select all

 
<?php
$headerArgs['title'] = "Page title";
$headerArgs['cssImports'] = "<link rel='stylesheet' type='text/css' href='/styles/somefile.css'/>";
$headerArgs['jsImports'] = "<script type='text/javascript' src='/scripts/somefile.js'></script>".
    "<script type='text/javascript' src='/scripts/someotherfile.js'></script>";
include "$_SERVER[DOCUMENT_ROOT]/blocks/header.php";
?>
...
<?php
include "$_SERVER[DOCUMENT_ROOT]/blocks/footer.php";
?>
 
The only thing I'm wondering is what to call this array. I'm thinking of calling it just $header instead of $headerArgs. Which would you call it?

Thanks for your reply tasairis.

Re: How do you template your site?

Posted: Thu Sep 10, 2009 1:41 am
by Weiry
JellyFish wrote: The only thing I'm wondering is what to call this array. I'm thinking of calling it just $header instead of $headerArgs. Which would you call it?
usually something simple would be best, i would probably name it as $headerArray

I also like the idea of storing your javascript links and css links too, so if later you needed more than one, you could write a foreach loop to print them all

Code: Select all

 
$headerArgs['jsImports'] = "<script type='text/javascript' src='/scripts/somefile.js'></script>".
    "<script type='text/javascript' src='/scripts/someotherfile.js'></script>";
 
you could in effect change to

Code: Select all

 
$headerArray['jsImports'] = array( "somefile.js", "someotherfile.js" );
foreach($headerArray['jsImports'] as $jsImport){
   print <script type='text/javascript' src='/scripts/{$jsImport}'></script>";
}
 
you could also change the jsImports array to store the path as well, so

Code: Select all

 
$headerArray['jsImports'] = array( "/path/somefile.js", "/scripts/someotherfile.js" );
print <script type='text/javascript' src='{$jsImport}'></script>"; //example print
 
Thats my 2 cents at least ^^

Re: How do you template your site?

Posted: Thu Sep 10, 2009 2:35 am
by JellyFish
That's true Weiry, maybe an array of URLs for the imports would be a better solution then having to write out all the HTML. But then you'd be loosing the ability to change certain attributes for each script element, but I guess there is nothing that you'd need to change there, atleasst I can't think of any.