Re: I need some thoughts on code duplication and separation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Oct 21, 2010 at 04:05:50AM +0200, Rico Secada wrote:

> Hi.
> 
> I am working on a small system where I am both trying to avoid code
> duplication and at the same time I am trying to keep the presentation
> logic separated from the application logic.
> 
> I am using sessions and are avoiding "headers already sent" problem by
> keeping the HTML out of the application.
> 
> For example, I would like to have a common header.php file, but it is
> difficult to create this since one file needs to have some specific
> Javascript located in the <head> </head> tags, but the other files
> doesn't need this.
> 
> Another file needs to have a specific "onload" call in the <body> tag,
> while yet another file also needs to have an "onload" call, but with
> different attributes.
> 
> I have been looking around in other systems to see what kinds of
> solutions are being used - as inspiration.
> 
> I have been thinking about the following solutions:
> 
> 1. Create only ONE header.php file that contains a lot of conditionals
> depending on what file is including it - the output of HTML/Javascript
> changes.
> 
> I believe this would turn into a very ugly hack. Difficult to maintain.

Not really. Here's what I do. I have a page controller which defines
variables and such, and then calls the header.php file. The page
controller will contain something like this:

$meta['jsfiles'] = 'onload.js';

The header.php will contain code like this:

<?php if (!empty($meta['jsfiles'])): ?>
<?php include $meta['jsfiles']; ?>
<?php endif; ?>

The page controller can also contain a variety of other settings, like:

$meta['content'] = 'cust_add.php';

and the header.php will contain:

<?php include $meta['content']; ?>

This directs the proper internal content for the header.php, which is
really like a "template" file.

Also remember that at the bottom of the page controller, you do a like
like this:

include 'header.php';

You can change this as you like for any given page controller.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux