appobs Posted November 22, 2015 Share Posted November 22, 2015 (edited) 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 November 22, 2015 by appobs Quote Link to comment https://forums.phpfreaks.com/topic/299551-is-building-up-the-pagecontent-var-at-odds-with-using-includes-for-page-content/ Share on other sites More sharing options...
mac_gyver Posted November 22, 2015 Share Posted November 22, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/299551-is-building-up-the-pagecontent-var-at-odds-with-using-includes-for-page-content/#findComment-1526939 Share on other sites More sharing options...
Jacques1 Posted November 22, 2015 Share Posted November 22, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/299551-is-building-up-the-pagecontent-var-at-odds-with-using-includes-for-page-content/#findComment-1526950 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.