Execute PHP with file_get_contents
Posted: Tue Jan 31, 2012 8:34 pm
Long story short, I replace certain "items" in a template.php file with information gathered from a DB.
This seemed like a fairly good solution until I got a request to make a shoutbox...big problems now.
The only way I though of doing this was to customize the template and do the shoutbox in plain PHP. The ability to customize the templates by adding in your own PHP seems like a fairly good and usable thing. However, echoing the actual contents of the file doesn't work...
So I tried using file_put_contents instead of echoing the str_replace and then including the template files but that means when someone updates the information in the DB they have to remove all the current information in the template by hand and then add the array_items in it again; so this didn't work.
Example template,
$HEADER = file_get_contents(HEADER); $MAIN = file_get_contents(MAIN); $FOOTER = file_get_contents(FOOTER); echo str_replace($array_items, $merged, $HEADER); echo str_replace($array_items, $merged, $MAIN); echo str_replace($array_items, $merged, $FOOTER);where array_items are the items in the template file (e.g. {page_title}) and merged is a merged array with the information from the DB.
This seemed like a fairly good solution until I got a request to make a shoutbox...big problems now.
The only way I though of doing this was to customize the template and do the shoutbox in plain PHP. The ability to customize the templates by adding in your own PHP seems like a fairly good and usable thing. However, echoing the actual contents of the file doesn't work...
So I tried using file_put_contents instead of echoing the str_replace and then including the template files but that means when someone updates the information in the DB they have to remove all the current information in the template by hand and then add the array_items in it again; so this didn't work.
Example template,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" /> <meta name="description" content="{meta_desc}" /> <meta name="keywords" content="{meta_keys}" /> <meta name="author" content="{author}" /> <title>{page_title}</title> </head> <body> <h1>{site_name}</h1> <div> <h2>{page_header}</h2> <p> {page_content} </p> </div> <div> <p> (C) MyCompany.com 2012 </p> </div> </body> </html>So here I am; asking for help on how to solve this in an efficient way.