www.webdeveloper.com
Results 1 to 5 of 5

Thread: Resequencing echo statements and includes: suggestions needed

  1. #1
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62

    Resequencing echo statements and includes: suggestions needed

    Dear PHP-ologists,

    For a variety of reasons (including legacy code) I have a number of websites that (in a nutshell) more or less do the following:

    In index.php:
    include ('templates/header.php');
    include ('content/homepage.php');
    include ('templates/footer.php');

    In header.php:
    echo $headerstuff_line1 . "\n";
    echo $headerstuff_line2 . "\n";
    echo $headerstuff_line3 . "\n";
    # etc...

    (and footer.php works similarly of course).

    In home.php:
    echo '<h1>Hello world</h1>' . "\n";
    echo 'Content line 1...' . "\n";
    echo 'Content line 2...' . "\n";
    echo 'Content line 3...' . "\n";
    # etc...

    And home.php (or any other page that gets included here, depending on the parsing of query parameters) may contain other stuff as well, such as SQL queries, invocation of E-mail / captcha / form / other classes, and what not. Crude but effective.

    But now I am faced with a need to modify the page header include file on the basis of variables that get defined in the content include file. For example in home.php there could be something like

    $pageTitle = 'Home';

    which would then have to be used in the header.php include, as in:

    echo '<title>' . $pageTitle . </title>' . "\n";

    Which means that I have no shot myself in the foot, since the header has been included (and has executed it's 'echo' statements) before the content file (with the declarations needed in the header) is loaded up only after that.

    Fixing it involves breaking my straightforward process flow - i.e. I would have to put the statements that output the header into the content include (e.g. home.php) which I find highly inelegant... Or even worse, I would have to store entire chunks of HTML content in large memory-consuming variables, manipulate them later, and output them only after modification, which also breaks my straightforward process flow _and_ means higher server load and delayed output and other impediments to performance that I don't want.

    So. Suggestions anyone? Clearly I need to so something very simple and sensible here, but i just can't see it. Any idea's would be appreciated!

    // Frank

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    I suppose the "best" solution would depend on how much time/work you can put into it. Perhaps the quickest would be to do an ob_start() at the beginning of the main script, then at whatever point you have all the data you need, do a ob_get_clean() and assign its result to a variable. Then you can use str_replace() or preg_replace() to change the values of the title, keywords, etc.; then echo that variable to output the page to that point.

    The opposite end of the spectrum would be to implement a MVC framework such as CodeIgniter, CakePHP, etc. or a templating system such as Smarty.

    Perhaps in between would be to functionalize your header stuff so that it can take parameters for the title and other variable data, then call the functions from the content page once it has the data?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62
    Hi, NogDog, and thanks for replying.
    Quote Originally Posted by NogDog View Post
    I suppose the "best" solution would depend on how much time/work you can put into it.
    Well, I have a number of legacy code chunks, and I'm somewhat lazy, so obviously I'm looking for a "least effort" optimum here... :-) But I realize that that may not be possible.

    Quote Originally Posted by NogDog View Post
    Perhaps in between would be to functionalize your header stuff so that it can take parameters for the title and other variable data, then call the functions from the content page once it has the data?
    I would rather output the header before getting to the content part. I'd like to keep that nice and separated - headers are headers, and part of the preliminaries to be completed before we concentrate on the content part and nothing else. Calling a function (or initiating another action) that causes the header to be rendered from within an include that should only contain the meat of page content strikes me as fundamentally wrong, inelegant, unintuitive, etc...

    Maybe I'm just too much of a purist... but experience tells me that if I'm unhappy with a certain approach and go ahead with it anyway, sometime later I am going to wish I hadn't. :-)

    Anyway, thanks for your suggestions. At least they will help me think!

    // Frank

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    This is probably a good illustration of why MVC and similar patterns try to separate the presentation layer (i.e. the View) from the other processing (i.e. the Model and Controller).

    Perhaps you can at least split your "content" into two files: one that does the "business process" and populates variables and arrays based on the request type/data, then you can include the header, content-output, and footer files, allowing them to access that data.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62
    Quote Originally Posted by NogDog View Post
    This is probably a good illustration of why MVC and similar patterns try to separate the presentation layer (i.e. the View) from the other processing (i.e. the Model and Controller).
    Agreed. It also illustrates the perils of having grown up in the procedural world of ANSI C and then, many years later, trying to plod through the 21st century as a self-taught PHP hack. :-)

    If I had followed a proper approach of functionalizing my output operations, using classes and objects instead of libraries and procedures, I would not be stuck like I am. But they didn't have them newfangled thingies in my youth, so when I jumped into PHP about 6 years ago I took the procedural approach, which now comes back to haunt me. :-)

    Quote Originally Posted by NogDog View Post
    Perhaps you can at least split your "content" into two files: one that does the "business process" and populates variables and arrays based on the request type/data, then you can include the header, content-output, and footer files, allowing them to access that data.
    The least inelegant kludge that I have managed to come up with is to keep a separate array of page request keys (the same used later on to include the appropriate content file) and use that to retrieve values for the various header properties. So before including the header and the pagecontent file specified by, say, $queryParms['p'] I will do something like

    list ($headerTitle, $headerKeywords, $headerDescription) = $headerStuff[$queryParms['p']];

    and then output the header. That should do the trick. Crude but effective.

    Thanks!!

    // Frank

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles