FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Templating





coreymanshack
So I have my template file I created a while back,
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.
thnn
Correct me if I am misinterpretting what you are trying to achieve.

Would you be wanting to replace the entire template, or just reuse the current one? Would it not simply be a matter of calling load again on another file, or even the same file?
imagefree
as thnn said, just load() another template file.
coreymanshack
Yea that'll probably work, I'll try it.
Related topics
Do you use css to design a website?
More forum types.
Would it be possible to use your free hosting to be used for
I want PHP Online Games
What's the best way to design a site?
PHPbb MOD
Webhosting Control Panel
Calendar with PHP
My recently redeveloped Flash Movie website!
mod_rewrite
Website themes in HTML???
[PHP] phpbb registration - add to other db
php/HTML pages question
phpBB reply error
Smarty Templating
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.