taith Posted November 24, 2006 Share Posted November 24, 2006 say we have template.html[code]<!-- Body --><table> <tr> <td></td> </tr></table>[/code]i want somthing that can load that file, then alter it(in realtime) to input variables[code]<!-- Body --><table> <tr> <td><?=$var?></td> </tr></table>[/code]any suggestions? Quote Link to comment Share on other sites More sharing options...
taith Posted November 24, 2006 Author Share Posted November 24, 2006 ok... lets simplify it down a bit... [code]<div><!-- body --></div>[/code]and gets recongized as[code]<div><?=$body?></div>[/code] Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 25, 2006 Share Posted November 25, 2006 place a marker variable or something like hte word bodythen do a string_replaceor whatever on it, that is what I had to do to create dynamic php on a file, I had to get the file, copy it, and edit it, ehre is the code if that helps.[code]<?phpsession_start();function read_file($file){ if(!function_exists("file_get_contents"))return file_get_contents($file); $ifile = fopen($file,"r"); $contents = false; if($ifile) while (!feof($ifile)) $contents .= fgets($ifile); fclose($ifile); return $contents;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><?phpinclude './includes/meta.inc.php';?></head><body><?phpinclude './includes/header.inc.php';?><!-- Begin Content --><div id="topimage"><img src="images/topimage.gif" alt="Face Image" /></div><div id="rightcontent"><?phpif ($_SESSION['controller'] == true) {mysql_connect("localhost", other);mysql_select_db("hasbadse_hbservice");// create user account.$errorhandler = "";$insert = "INSERT INTO userinfo SET username = '$_SESSION[username]', password = '$_SESSION[password]', email = '$_SESSION[email]', subdomainname = '$_SESSION[subdomain]';";mysql_query($insert);$select = "SELECT * FROM userinfo WHERE username = '$_SESSION[username]';";$query = mysql_query($select);if ($row = mysql_fetch_array($query)) { // retrieve userid with user account. $id = $row['id']; } // end getting userid.$header = $_SESSION['header'];$text = $_SESSION['text'];$insertcms = "INSERT INTO usercms (text, userid) VALUES ('$text', '$id');";if (!mysql_query($insertcms)) {$errorhandler .= "There was a problem inserting the Content Management Information.<br />";$errorhandler .= "Please contact support about this problem.<br />";}// VERY IMPORTANT, everything below is used for directory creation.$var = preg_replace('/[^\x09\x0A\x0D\x20-\x7F]/e', '"&#".ord($0).";"', $_SESSION['subdomain']);$dir = "userpages/$var";mkdir("$dir", 0700);if (!mkdir) {$errorhandler .= "unable to create the directory. Please contact support or try again.<br />";}$buffer = read_file($_SESSION['template']);$buffer = str_replace("hold", $id, $buffer);// here I can do all my changes to the buffer variable(which contains the file information)$outfile = fopen("userpages/{$var}/index.php", "w"); // open writeablefwrite($outfile, $buffer);fclose ($outfile);chmod("userpages/{$var}", 0755);chmod("userpages/{$var}/index.php", 0755);// VERY IMPORTANT, everything above here is used for directory creation.$insertdn = "INSERT INTO subdomain (path, subdomainname, userid) VALUES ('$dir', '$_SESSION[subdomain]', '$id');";if (!mysql_query($insertdn)) {$errorhandler .= "There was a problem, please contact support immediately.<br />";}// start check for error handlerif ($errorhandler != "") {echo "<span style=\"color:red;\">";echo $errorhandler;echo "</span>";}if ($errorhandler == "") {?><h4>Thank You</h4><p>Thank you for your purchase, you can go over to the administration page, and login there.</p><a href="/useradmin/useradmin.php" title="User Admin">User Admin</a><?php }}else {echo "You have to go through the entire signup process, before coming to this page, in order <br />";echo "for your account to be able to be created.<br />";}?><?phpinclude './includes/footer.inc.php';?></div></body></html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Thank You</title></head><body></body></html>[/code]You see what I mean, now however, here are my template pages, that I used, it shows you what I did in those pages, that allowed me to do my search and replace with regular expressionsThe template is chosen based on what they picked, for instance if they pick template1.php it takes it copies the information int eh file (the actual .php page itself), and saves it in another file, with the dynamic hcanges done to it. Works pretty smootherTemplate1.php[code]<?phpsession_start();mysql_connect("localhost", other);mysql_select_db("hasbadse_hbservice");?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><link rel="stylesheet" type="text/css" href="/templates/template1/css/globaltemplate1.css" /><title>Hasbadservice</title></head><body><div id="wrapper"> <!-- Entire Header Area --> <div id="header"> <div id="leftheader"><?php$selectuser = "SELECT * FROM userinfo WHERE id = 'hold';";$queryuser = mysql_query($selectuser);if ($row = mysql_fetch_array($queryuser)) {?><pre><?phpecho $row['subdomainname'] . ".\n";?>HasBadService.Com</pre><?php}?> </div> <img src="/templates/template1/images/star.gif" id="star" alt="Star Symbol" /> </div> <!-- End Header Area --> <div id="leftcolumn"> </div> <div id="rightcolumn"><!-- Not part of original template. This was added in dynamically --> <div class="content"><?php$select = "SELECT * FROM usercms WHERE userid = 'hold';";$query = mysql_query($select);while ($row = mysql_fetch_array($query)) {echo "<h4>" . stripslashes($row['header']) . "</h4>";echo "<p>" . stripslashes($row['text']) . "</p>";}?> </div><!-- End section that was done dynamically --> </div></div><hr /></body></html>[/code]template2.php[code]<?phpsession_start();mysql_connect("localhost", "hasbadse_joyel", "joyel");mysql_select_db("hasbadse_hbservice");?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><link rel="stylesheet" type="text/css" href="/templates/template2/css/globaltemplate2.css" /><title>Hasbadservice</title></head><body><div id="header"><?php$selectuser = "SELECT * FROM userinfo WHERE id = 'hold';";$queryuser = mysql_query($selectuser);if ($row = mysql_fetch_array($queryuser)) {echo strtoupper($row['subdomainname']) . ".HASBADSERVICE.COM";}?> <div id="bar"> </div></div><div id="wrapper"> <div id="leftbox"> <div id="upper"></div> <div id="lower"></div> </div> <div id="rightbox"> <!-- This section is not part of the original content, it's all dynamic --><div class="content"><?php$select = "SELECT * FROM usercms WHERE userid = 'hold';";$query = mysql_query($select);while ($row = mysql_fetch_array($query)) {echo "<h4>" . stripslashes($row['header']) . "</h4>";echo "<p>" . stripslashes($row['text']) . "</p>";}?></div><!-- End dynamic addition --> </div></div><div id="footer"></div></body></html>[/code]As you can see, it is a bad way for some things,f or instance, if the database information chagnes all the old templates are lost, the people who created accounts, that's what my new "config" file is for instead. BUt it gives you the idea of what you wanted to do. Quote Link to comment Share on other sites More sharing options...
taith Posted November 25, 2006 Author Share Posted November 25, 2006 here we go... for all who wanted to see...[code]<?$body=test;$handle = @fopen("test.html", "r");if($handle){ while(!feof($handle)) $buffer[] = fgets($handle, 4096); fclose($handle);}foreach($buffer as $line){ if(strstr($line, '<!--')&&strstr($line, '-->')){ $len=strlen($line); $before=substr($line, 0, strpos($line,"<!--")); $mid = substr($line, strpos($line,"<!--")+4); $mid=substr($mid,0,strpos($line,"-->")-10); $mid=trim($mid); $after=substr($line,strpos($line,"-->")+3); echo $before.$$mid.$after; }else echo $line;}?> [/code]then in test.html[code]<table> <tr> <td><!-- body --></td> </tr></table>[/code] Quote Link to comment Share on other sites More sharing options...
redbullmarky Posted November 25, 2006 Share Posted November 25, 2006 something to think about though - is typing:[code]<td><?=$body ?></td>[/code]really much worse than typing[code]<td><!-- body --></td>[/code]considering the former is just as clean and doesnt come with the performance hit of the latter? Quote Link to comment Share on other sites More sharing options...
taith Posted November 25, 2006 Author Share Posted November 25, 2006 yes... but the whole idea here is inforcing templates...[code]<?$body=test;$required=array('body','head');$handle = @fopen("test.html", "r");if($handle){ while(!feof($handle)) $buffer[] = fgets($handle, 4096); fclose($handle);}foreach($buffer as $line){ if(strstr($line, '<!--')&&strstr($line, '-->')){ $len=strlen($line); $before=substr($line, 0, strpos($line,"<!--")); $mid = substr($line, strpos($line,"<!--")+4); $mid=substr($mid,0,strpos($line,"-->")-10); $mid=trim($mid); $i[$mid]=true; $after=substr($line,strpos($line,"-->")+3); $temp .= $before.$$mid.$after; }$temp .= echo $line;}foreach($required as $k){ if(!isset($i[$k])) $failed=true;}if($failed){ $temp=''; require('template/default.php');}?>[/code]script isnt tested... but thats the jist... Quote Link to comment Share on other sites More sharing options...
redbullmarky Posted November 27, 2006 Share Posted November 27, 2006 taith, i know what youre getting at. but its well documented that PHP itself is a perfectly good templating engine itself. the short tags + alternative syntax are more than adequate. i'm sure a few people have noticed me point them towards this article before:http://www.massassi.com/php/articles/template_engines/which not only explains why template engines are not all that great, but also explains how to STILL use this whole templating concept but without the bloat/overhead.tis a good read, even if you're adament not to go down that road. Quote Link to comment Share on other sites More sharing options...
taith Posted November 28, 2006 Author Share Posted November 28, 2006 yes... good read... however the code completly conflicts with the rest of my programming... 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.