Jump to content

Is "building up the pageContent var" at odds with using includes for page content?


Recommended Posts

Edit: I'm just off to look up 'storing a function in a variable', thought I'd post anyway, brb!!

 

Hi all, back for more!

 

I'm working on my standard php script, it goes something like:

bootstrap
settings 
build page content vars
HTML5 

So I build a variable like $pageContent then echo it in the HTML bit.

 

Thing is, in trying to make an automated menu, I think two things I'm doing turn out to be at odds with each other.

 

 

The menuer() does:

  • Looks in a $dir for files (includes)
  • returns an unordered list with 'query-links' like:

?p=$nameOfIncludeFile

Page content generator (previous version) does:

[html before]

<? if($_GET) {
    include($dir . $_GET['p']);
} ?>

[html after]

Building the var first, the IF wouldn't be in the HTML:

(Cleaning up the HTML is largely the point)

[html before]

<? echo $pageContent; ?>

[html after]

But I don't think I can:

  • Use the contents of the include file to build $pageContent
  • Put a function after $pageContent .=
  • Store an actual function in a variable (otoh, brb)
Edited by appobs

i think (it's not entirely clear what you are asking or what problem you are having with your code) you are building up the entire content in a single variable and have a 'cart before the horse problem'. you should instead build up each different piece of the content in a separate variable, then combine the variables at the end or just output each separate variable in your html document at the appropriate place. build up the navigation in its own variable, so that all the logic that contributes to the navigation can add to just the navigation.

You should consider using a template engine like Twig or Smarty.

 

Those PHPHTML gymnastics are fine for very simple pages, but when you're dealing with more complex structures, it's time to get rid of the spaghetti code and use a more systematic approach.

 

Template engines were made for exactly this purpose. You can easily insert one template into another (e. g. a menu into a page), and you can even retroactively inject new HTML snippets into an existing HTML structure.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.