So I have my template file I created a while back,
I've used it successfully many times like this
But I was thinking, what if I want to trash the template, re-load it, and start replacing variables again midsource. How would I got about doing that.
| Code: |
| <?php
class Template { var $template; function load($filepath) { $this->template = file_get_contents($filepath); } function replace($var, $content) { $this->template = str_replace("#$var#", $content, $this->template); } function amend($var, $content){ //decare staticstate static to save state between function calls. static $staticstate; if($staticstate!=""){ //after the first run through of this function everything goes here //temp1 is created to know what needs to be replaced $temp1 = $staticstate; //staticstate is amended so the correct thing is replaced. $staticstate .= $content; $this->template = str_replace($temp1, $staticstate, $this->template); } else{ //the real action starts here, and staticstate is given some content, and the string is replaced as normal. $staticstate = $content; $this->template = str_replace("#$var#", $content, $this->template); } } function publish() { eval("?>".$this->template."<?"); } } ?> |
I've used it successfully many times like this
| Code: |
| <?php
$config = include'../../include/config.php'; $templateclass = include'../../include/class/template.class.php'; $creature = include'creature.php'; $template = new Template(); $template->load("../../html/mechanics/monsters/creature.html"); session_start(); //vars $username = $_SESSION['username']; $password = $_SESSION['password']; $meleeattack = $_POST['meleeattack']; //connect and get chardata mysql_connect("$dblocation", "$dbuser", "$dbpass") or die(mysql_error()); mysql_select_db("$dbname") or die(mysql_error()); $result = mysql_query("SELECT * FROM $dbtableusers WHERE username='$username' AND password='$password'"); while ($data = mysql_fetch_array($result)){ $chardata = $data['chardata']; } $chardata = unserialize($chardata); //if life is 0, character can not do anything if($chardata[0]['life'] <= 0){ $template->replace("areaname", "Health Low"); $template->replace("text", "Your health is to low to continue on, you might try resting in an inn."); $template->replace("backtoarea", "<a href=\"../../mechanics/areas/mountains.php\">Go Back to the Mountains</a>"); $template->publish(); exit; } //set creature vars //set up creature life $ctrlife = rand(25, 50); //set creature name $randomname = rand(1, 3); if($randomname == 1){ $ctrname = "Black Bear"; } else if($randomname == 2){ $ctrname = "White Bear"; } else if($randomname == 3){ $ctrname = "Sandy Bear"; } //set creature damage type and number $ctrdmgtype = "Melee"; $ctrdmglow = 1; $ctrdmghigh = 3; //call creature function Creature($ctrlife, $ctrname, $ctrdmgtype, $ctrdmglow, $ctrdmghigh, $meleeattack, $username, $password, $templateclass, $template); $template->publish(); ?> |
But I was thinking, what if I want to trash the template, re-load it, and start replacing variables again midsource. How would I got about doing that.
