darth_tater Posted May 3, 2007 Share Posted May 3, 2007 My website's content is, as of now, hard coded but I wish to change this (updating ~ 30 pages become quite tedious). My basic layout can simply be described as a header with left navigation and footer, with all the main content going into the second column on the right. As of now, i am building my website to use a modular approach, and this is how I was thinking of serving all my content. Each page comes from at least two PHP files: 1) the template.php file which contains the basic header, navigation, and footer 2) The actual content page and its associated functions (about us.php, home.php, contact us.php, etc) this way, I can change the content/function of each page w/o having to worry about the static layout that must be the same for each footer or header…and I can also specify where each content comes from EG: the news.php pulls things from a database, the contqactus.php parses a text file, etc… My questions are as follows: 1) is there a better way to do it than above, and if so what is it. 2) The method I’ve outlined above must be able to inject certain things into the default template.php to change the layout of each specific page… how can I best do this? Link to comment https://forums.phpfreaks.com/topic/49759-what-is-the-best-way-to-build-each-page-useing-a-generic-template/ Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 You could use a template parser, which would probably be a little too heavy for your situation. You could just str_replace some very obscure texts to insert content, or you could have the files leading up to where you want the content to be included above and below you included content pages (doubt that made sense).... As for the default template.php you could have something like this: template.php <html> <head> <title><?php echo $title; ?></title> <!-- more html and what not --> index.php $title = "Main Page!"; include("header.php"); include("some content page"); include("footer.php"); Link to comment https://forums.phpfreaks.com/topic/49759-what-is-the-best-way-to-build-each-page-useing-a-generic-template/#findComment-244064 Share on other sites More sharing options...
darth_tater Posted May 3, 2007 Author Share Posted May 3, 2007 so if im looking at your post correctly, the template.php will inherit the variables of the index.php (or w/e file included the template)? Link to comment https://forums.phpfreaks.com/topic/49759-what-is-the-best-way-to-build-each-page-useing-a-generic-template/#findComment-244071 Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 Yeah.... Any file included has access to any variables previously set. You could also do it like this: template.htm <html> <head> <title>{TITLE}</title> </head> <body> <div id="header"> your header crap here </div> maybe some nav stuff here or something <div id="content"> {CONTENT} </div> </body> </html> </body> index.php function ParseFile($file, $vars) { $file1 = implode("\r\n", $file); foreach($vars as $k => $v) { $file1 = str_replace("{".strtoupper($k)."}", $v); } return $file1 } include("content.php"); ParseFile("template.htm", $replace); content.php //let's pretend this is a login page just so I can have some content for an example $loginform = '<form action="" method="post"> Username: <input type="text" name="username"><br /> Password: <input type="password" name="password"><br /> <input type="submit" value="login"> </form>'; $pagetitle = "Login Page"; $replace = array('title' => $pagetitle, 'content' => $loginform); This would create <html> <head> <title>Login Page</title> </head> <body> <div id="header"> your header crap here </div> maybe some nav stuff here or something <div id="content"> <form action="" method="post"> Username: <input type="text" name="username"><br /> Password: <input type="password" name="password"><br /> <input type="submit" value="login"> </form> </div> </body> </html> That's the general idea.... You could also do it where you just pass variables though and include parts of pages instead of entire pages like that. (PS if you decide to go that approach, you probably don't wanna use that script.... If you decide to go that approach look for a template parser such as Smarty) Link to comment https://forums.phpfreaks.com/topic/49759-what-is-the-best-way-to-build-each-page-useing-a-generic-template/#findComment-244076 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.