/    Sign up×
Community /Pin to ProfileBookmark

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

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 15.2010 — 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?
Copy linkTweet thisAlerts:
@frankvwauthorJul 16.2010 — Hi, NogDog, and thanks for replying.
I suppose the "best" solution would depend on how much time/work you can put into it.[/QUOTE]
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.

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?[/QUOTE]
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
Copy linkTweet thisAlerts:
@NogDogJul 16.2010 — 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.
Copy linkTweet thisAlerts:
@frankvwauthorJul 16.2010 — 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).[/quote]
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. :-)

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.[/QUOTE]
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
×

Success!

Help @frankvw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...