Hi guys, I've learned php but I still do not know a reliable solution to seperate code from design. I already know i can use includes and CSS to clean the page but.. something like this.. (please be patient with me ) I want this to be the index.php for example; <html> <head> <title>{TITLE}</title> </head> <body> <div>Logo</div> {DynamicText} </body> </html> I see this in a lot of programs, how can I make my template files real simple not the ugly way.. basically, how to put the php code in one place, and the template files in another, but still be able to call any php function during writing my templates.. like the one above.. look at the ugly way to do it. <html> <head> <title><?php include ('lang.php');?><?echo $title;?> I want to avoid this and put a simple {TITLE} where title dynamically goes to another php file and grab the title variable and put it in a fast efficient way. I am developing a small application and this would save me a lot of time really. Any simple, straightforward help / input?
Haha, I guess what you need is a template engine. Look at Smarty, a very powerful PHP template engine. www.smarty.net
You may want to look into MVC (Model, View, Controller) style coding. The purpose of MVC is to keep design, and controls separate which makes much more transportable applications. Most of the production frameworks use MVC style formatting, and you can make your own MVC framework if you don't need a full framework.
My application is quite small and Smarty is for large/high traffic sites I hear. Do you still think Smarty is what I am looking for?! All repped.
Listen to suggestion jestep's about the MVC paradigm But if you want a basic template system as you suggested, a few lines of code can accomplish that. I have a system I wrote laying on my computer, when I get off of work I'll send you it.
I actually do something like this on my websites. I make an index.php file which is very small. It simply includes the main component PHP files which have all the functions/classes/etc and it grabs what page they're trying to access from the vars in the URL and then it initiates an output buffer, includes that file, and stores the file's contents in a variable with ob_get_contents() and then I end that buffer and do the same thing with the main template file and the main template file simply has vars in it that are replaced with whatever was already set in the rest of the index file. Easier way to output variables onto a page with PHP though is <?=$var?>, looks so much nicer than <?echo $var;?> Here is a real example of what my index file is on one of my sites: <? DEFINE("IS_INDEX", "1"); include("./include.php"); if(!defined("INCLUDE_FILE")) die("Could not load config file."); $url_var_value = $_GET[$config->url_var]; $url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; if(empty($url_var_value)) $url_var_value = "main"; $requested_page = $config->pages_dir . $url_var_value . ".php"; if(!file_exists($requested_page)) $requested_page = $config->pages_dir . "error.php"; $navigation = array(); $sidemenu = array(); $header = GetContents("./head.php"); $content = GetContents($requested_page); $body = GetContents("./layout.php"); print $body; ?> PHP: and here is the GetContents function in my include.php file function CreateGlobals() { #THIS FUNCTION WILL EASILY CREATE A LINE OF CODE #TO GLOBAL EVERY VARIABLE THAT CURRENTLY EXISTS #IN THE GLOBAL SCOPE SO THAT IT CAN BE USED IN #THE LOCAL SCOPE WHERE YOU CALL THIS FUNCTION #USAGE: eval(CreateGlobals()); return 'global $' . implode(array_keys($GLOBALS), ',$') . ';'; } function GetContents($file) { eval(CreateGlobals()); ob_start(); include($file); $contents = ob_get_contents(); ob_end_clean(); return $contents; } PHP: In there is also my CreateGlobals function which a lot will argue you should NEVER use but frankly I need EVERY public variable usable in my included files since they're files like my layout and other page files. Good luck.