Re: [PHP] I need some thoughts on code duplication and separation

2010-10-24 Thread Rico Secada
On Thu, 21 Oct 2010 10:55:14 -0400
Paul M Foster pa...@quillandmouse.com wrote:

 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

Thanks Paul! It's a nice way to do it.

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



Re: [PHP] I need some thoughts on code duplication and separation

2010-10-21 Thread Peter Lind
On 21 October 2010 04:59, David McGlone da...@dmcentral.net wrote:
 On Thu, 2010-10-21 at 04:05 +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.

 2. Create a HTML generating class with a set of methods that each
 contains an adequate amount of parameters. Each method maintains its
 own HTML tag. For example, docType($type) would generate the doctype
 specification.

 I believe this is a cleaner solution, but the problem with code
 duplication isn't avoided.

 Some of the presentation logic contains conditionals and the HTML
 changes when the conditional changes, hence the header content changes,
 but the doctype, html, and head doesn't necessarily change and
 they would get duplicated a couple of times in some files.

 3. Avoid the problem all together, use output buffering and completely
 forget about separation between application and presentation.

 I hope I make sense.

 Any thoughts on these kinds of problems?

 sounds like a job for smarty http://www.smarty.net/

 I've used smarty and it's nice.


How does Smarty solve this problem? Just curious.

Regards
Peter

-- 
hype
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
/hype

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



Re: [PHP] I need some thoughts on code duplication and separation

2010-10-21 Thread a...@ashleysheridan.co.uk
Bit late to this one, but what about an mvc framework like codeigniter? It's 
perfectly acceptable to use if statements and loops in the view, which would 
solve your problem. You can set a trigger variable in the controller, and in 
the header view, check to see if its set and output your javascript lines as 
appropriate.

Thanks,
Ash
http://www.ashleysheridan.co.uk

- Reply message -
From: Peter Lind peter.e.l...@gmail.com
Date: Thu, Oct 21, 2010 07:01
Subject: [PHP] I need some thoughts on code duplication and separation
To: David McGlone da...@dmcentral.net
Cc: php-general@lists.php.net


On 21 October 2010 04:59, David McGlone da...@dmcentral.net wrote:
 On Thu, 2010-10-21 at 04:05 +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.

 2. Create a HTML generating class with a set of methods that each
 contains an adequate amount of parameters. Each method maintains its
 own HTML tag. For example, docType($type) would generate the doctype
 specification.

 I believe this is a cleaner solution, but the problem with code
 duplication isn't avoided.

 Some of the presentation logic contains conditionals and the HTML
 changes when the conditional changes, hence the header content changes,
 but the doctype, html, and head doesn't necessarily change and
 they would get duplicated a couple of times in some files.

 3. Avoid the problem all together, use output buffering and completely
 forget about separation between application and presentation.

 I hope I make sense.

 Any thoughts on these kinds of problems?

 sounds like a job for smarty http://www.smarty.net/

 I've used smarty and it's nice.


How does Smarty solve this problem? Just curious.

Regards
Peter

-- 
hype
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
/hype

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



Re: [PHP] I need some thoughts on code duplication and separation

2010-10-21 Thread Paul M Foster
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



Re: [PHP] I need some thoughts on code duplication and separation

2010-10-21 Thread David McGlone
On Thu, 2010-10-21 at 08:01 +0200, Peter Lind wrote:
 On 21 October 2010 04:59, David McGlone da...@dmcentral.net wrote:
  On Thu, 2010-10-21 at 04:05 +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.
 
  2. Create a HTML generating class with a set of methods that each
  contains an adequate amount of parameters. Each method maintains its
  own HTML tag. For example, docType($type) would generate the doctype
  specification.
 
  I believe this is a cleaner solution, but the problem with code
  duplication isn't avoided.
 
  Some of the presentation logic contains conditionals and the HTML
  changes when the conditional changes, hence the header content changes,
  but the doctype, html, and head doesn't necessarily change and
  they would get duplicated a couple of times in some files.
 
  3. Avoid the problem all together, use output buffering and completely
  forget about separation between application and presentation.
 
  I hope I make sense.
 
  Any thoughts on these kinds of problems?
 
  sounds like a job for smarty http://www.smarty.net/
 
  I've used smarty and it's nice.
 
 
 How does Smarty solve this problem? Just curious.

from what I understood from his post He mentions he wants to separate
the application and presentation, code duplication is avoided and it's a
cleaner solution and #2 above sounds like he's describing a smarty
template.

I'm not 100% positive it will help him completely, but it does do a lot
of the things he mentions.



-- 
Blessings,
David M.


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



[PHP] I need some thoughts on code duplication and separation

2010-10-20 Thread Rico Secada
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.

2. Create a HTML generating class with a set of methods that each
contains an adequate amount of parameters. Each method maintains its
own HTML tag. For example, docType($type) would generate the doctype
specification.

I believe this is a cleaner solution, but the problem with code
duplication isn't avoided.

Some of the presentation logic contains conditionals and the HTML
changes when the conditional changes, hence the header content changes,
but the doctype, html, and head doesn't necessarily change and
they would get duplicated a couple of times in some files.

3. Avoid the problem all together, use output buffering and completely
forget about separation between application and presentation.

I hope I make sense.

Any thoughts on these kinds of problems?

Best regards.


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



Re: [PHP] I need some thoughts on code duplication and separation

2010-10-20 Thread David McGlone
On Thu, 2010-10-21 at 04:05 +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.
 
 2. Create a HTML generating class with a set of methods that each
 contains an adequate amount of parameters. Each method maintains its
 own HTML tag. For example, docType($type) would generate the doctype
 specification.
 
 I believe this is a cleaner solution, but the problem with code
 duplication isn't avoided.
 
 Some of the presentation logic contains conditionals and the HTML
 changes when the conditional changes, hence the header content changes,
 but the doctype, html, and head doesn't necessarily change and
 they would get duplicated a couple of times in some files.
 
 3. Avoid the problem all together, use output buffering and completely
 forget about separation between application and presentation.
 
 I hope I make sense.
 
 Any thoughts on these kinds of problems?

sounds like a job for smarty http://www.smarty.net/

I've used smarty and it's nice.

-- 
Blessings
David M.


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



RE: [PHP] I need some thoughts on code duplication and separation

2010-10-20 Thread Tommy Pham
 -Original Message-
 From: David McGlone [mailto:da...@dmcentral.net]
 Sent: Wednesday, October 20, 2010 7:59 PM
 To: php-general@lists.php.net
 Subject: Re: [PHP] I need some thoughts on code duplication and separation
 
 On Thu, 2010-10-21 at 04:05 +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.
 
  2. Create a HTML generating class with a set of methods that each
  contains an adequate amount of parameters. Each method maintains its
  own HTML tag. For example, docType($type) would generate the doctype
  specification.
 
  I believe this is a cleaner solution, but the problem with code
  duplication isn't avoided.
 
  Some of the presentation logic contains conditionals and the HTML
  changes when the conditional changes, hence the header content
  changes, but the doctype, html, and head doesn't necessarily
  change and they would get duplicated a couple of times in some files.
 
  3. Avoid the problem all together, use output buffering and completely
  forget about separation between application and presentation.
 
  I hope I make sense.
 
  Any thoughts on these kinds of problems?
 
 sounds like a job for smarty http://www.smarty.net/
 
 I've used smarty and it's nice.
 
 --
 Blessings
 David M.
 
 

Thanks David!  I was looking for something like that too!

Regards,
Tommy


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