replacing variables

Discussion in 'PHP' started by decepti0n, Dec 30, 2006.

  1. #1
    I started working on a templates class, and the template info is stored in the db.

    Templates.Class.php:
    class Template
    {
      	var $info = array();
      	
    	function Template($template)
    	{
    	  	$file = "templates/$template.config.php";
    	  	if (file_exists($file))
    	  	{
    		    require($file);
    		    $this->info = new Temp();
    		    //var_dump($this->info);
    		    //print_r($this->info);
    		}
    		else
    		{
    		  	echo "No Template: $template<br />File: $file";
    		}
    	}  	
    	
    	function render_part($part)
    	{
    	  	//highlight_string($this->info->info22[$part]);
    	  	if (array_key_exists($part, $this->info->info22))
    	  	{
    	  		return $this->info->info22[$part];
    	  	}
    	  	else
    	  	{
    		    return "There is not template part '$part'";
    		}
    	}
    }
    PHP:
    default.config.php (what is called when I call new Template(), just creates an array holding all the template info for the style 'default'
    class Temp
    {
      	var $template_name;
      	var $info22;
      	
      	function Temp()
      	{
    		$this->template_name = "default";	
    		$info2 = mysql_query("SELECT * FROM templates WHERE tname = '$this->template_name'");	   
    		while ($t = mysql_fetch_array($info2))
    		{
    		  	$this->info22[$t['tpart']] = $t['temp'];
    		}
    		//echo "\$this->info22[\$t['tpart']] = \$t['temp'];<br />";
    		return $this->info22;		
    	}
    }
    PHP:
    and just the bit in header.php that starts the whole thing:
    $basedir = "http://localhost/noc";
    $gametitle = "Nation of Chaos - Beta - ".uNAME;
    $style_dir = "$basedir/style2.css";
    
    require("classes/Template.Class.php");
    $t = new Template("default");
    
    require('init.php'); // just holds the $settings array
    
    eval("\$start = \$t->render_part(\"page_start\");");
    echo $start;
    PHP:
    It loads up the code fine, but doesn't replace the variables, so it still echoes out:
    <head>
    <title>$settings['page_title']</title>
    Code (markup):
    ^^ the same as it is in the database.

    I've tried 144 ways to do it, but nothing will work for me. I've tried using eval too, since that worked on my last project, but no luck.

    Sorry for the extensive code, but if anyone can help me out i'd be very thankful :)
     
    decepti0n, Dec 30, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Try taking out the sinlge quotes out of the array brackets.

    
    <title>$settings[page_title]</title>
    
    Code (markup):
     
    nico_swd, Dec 30, 2006 IP
  3. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #3
    echo stripslashes($start);

    Taking the quotes out does absolutely nothing.
     
    KalvinB, Dec 30, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    ^^ Actually, it may does cause I had the same issue with a template system that I did some time ago...

    
    $test['yeah'] = 'Test';
    
    echo "$test['yeah'] string "; // Doesn't work
    echo "$test[yeah] string "; // Works
    echo "{$test['yeah']} string "; // Works
    
    
    PHP:
     
    nico_swd, Dec 30, 2006 IP
  5. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #5
    that makes sense.
     
    KalvinB, Dec 30, 2006 IP
  6. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Still nothing :(

    I tried checking the Mybb source (which is where I got the idea from) but that didn't help either.
     
    decepti0n, Dec 30, 2006 IP