Omzy Posted September 8, 2009 Share Posted September 8, 2009 Basically I just need a quick, easy and simple way of separating my PHP code from my HTML code. So I wanna have one file with all the HTML and one file with the PHP code, linked via an include. I'm not after a whole templating system like Smarty, just something simple that I can code by myself. Are there any online tutorials on how to do this? Quote Link to comment Share on other sites More sharing options...
sKunKbad Posted September 8, 2009 Share Posted September 8, 2009 I don't know if you would consider any frameworks like CodeIgniter, but frameworks are designed to do just what you are asking. Will you ever need to use php inside your HTML? If so, a framework, or Smarty might be the way to go. Quote Link to comment Share on other sites More sharing options...
Omzy Posted September 8, 2009 Author Share Posted September 8, 2009 No Frameworks, no templating systems. Just a short piece of code that will do the job for me, I hope! Quote Link to comment Share on other sites More sharing options...
Alex Posted September 8, 2009 Share Posted September 8, 2009 You could do something really simple like this: $templateObjects = Array('somefile' => Array('{REPLACE_ME1}', '{REPLACE_ME2}')); class Template { private $template; function __construct($title) { $this->template = str_replace('{TITLE}', $title, file_get_contents('template/html/main.html')); } public function setContent($section, $content) { $this->template = str_replace($section, $content, $this->template); return true; } public function display() { echo $this->template; return true; } } function getContent($template, $content) { global $templateObjects; $structure = @file_get_contents('template/html/' . $template . '.html'); if(!$structure) return 'Error: Invalid template id (' . $template . ')'; return str_replace($templateObjects[$template], $content, $structure); } Then to use it: template/html/main.html defining your doctype etc.. everything that will be the same on every page.. <head> <title> {TITLE} </title> <html> <body> {MAIN_CONTENT} </body> </html> template/html/somefile.html This is some file.. {REPLACE_ME1} {REPLACE_ME2} file.php //Include class file $template = new Template('Some title'); $content = getContent('somefile', Array('Hello', 'World')); $template->setContent('{MAIN_CONTENT}', $content); $template->display(); It's just a quick example, should you get started. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted September 9, 2009 Share Posted September 9, 2009 try smarty http://www.smarty.net/crashcourse.php Quote Link to comment Share on other sites More sharing options...
trq Posted September 9, 2009 Share Posted September 9, 2009 No Frameworks, no templating systems. Just a short piece of code that will do the job for me, I hope! There is no such thing. You can separate programming logic from presentation logic through good design, this however is not the same as removing php completely from markup. To do that, you must use a template engine of some sort which just adds extra overhead really. Quote Link to comment Share on other sites More sharing options...
thewooleymammoth Posted September 9, 2009 Share Posted September 9, 2009 like... <?php if(1==1) require_once("HTMLFIlE.hmtl"); else require_once("otherfile.html"); ?> ???? Quote Link to comment 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.