Jump to content

Contents not getting passed


Archadian

Recommended Posts

Sorry in advance for the long post:

 

The contents is not getting passed here are the websites:

 

http://phpweb.no-ip.com/index.php?page=login

http://phpweb.no-ip.com/index.php

 

Here is the code:

 

index.php

 

$temp_dir = dirname(__FILE__) . '\\templates\\';

$get_page =& new Template();

$page = $_GET['page'];

if (!isset($_GET['page'])) {

$page = $temp_dir . "index.html";

}
else {

$page = $temp_dir . $page . '.html';
}

$get_page->set_template($page);


if ($page === 'login.html') { // Just the login template (login.html)

$get_page->page('TITLE',  "Login"); 
$get_page->page('T_USERNAME',  "Username:");
$get_page->page('T_PASSWORD', "Password:"); 
$get_page->page('B_LOGIN', "Login");

}

$content = $get_page->parse(); 
$page_title = "Title Here";

$interface =& new Template();
$interface->set_template($page);
$interface->page('CONTENT',    $content); 
$interface->page('PAGE_TITLE', $page_title);
print $interface->parse();

 

class.php

 

class template
{

protected $template; 			//The template name
protected $temp_string;    	//The template string will be stored here
protected $prefix    = "{"; 		//The variable prefix character.
protected $suffix    = "}"; 		//The variable suffix character.
protected $items = array(); 	//The values of your variables will be stored here

public function set_template($filename)
{

	$this->template = $filename; 
}

public function page($name, $value)
{

	//storing our variable and its value into an array
	$this->items[$name] = $value; 
}

public function load_file()
{

	//saving its contents into a string		
	$this->temp_string = file_get_contents($this->template);
}

public function replace_items()
{

	//The foreach loop is very useful when we want to loop through
	//associative arrays. 
	foreach($this->items as $assoc => $value)
	{
		//appending the variable prefixes to match. ex: {VAR_NAME} 
		$name = $this->prefix.$assoc.$this->suffix; 

		//replacing every instance of the variable with it's corresponding value. 
		//so {VAR_NAME} becomes "this is my variable" 
		$this->temp_string = str_replace($name, $value, $this->temp_string); 
	}
}

 public function parse()
{

	//loading the file 
	$this->load_file(); 

	//setting the variables 
	$this->replace_items(); 

	//outputting the newly parsed template 
	return $this->temp_string; 
}
}

 

If the index.html and login.html code is needed just let me know. Anyways...the contents is not getting passed as you have seen in the web links i provided above. What am i doing wrong? If anyone can help with this problem i HIGHLY appreciate it. Thanks.

Link to comment
Share on other sites

Ok 8 lines up from the bottom of index.php i put this:

 

$content = $get_page->parse();
echo 'This is the content: ' . $content;

 

and when it echoed $content it was right...the content got passed in $get_page->parse();. So whats going on that would cause nothing to show up on the links in the first post i made? I have revised the login link so you can see what its doing now. Thanks

Link to comment
Share on other sites

Hi, I find your code confusing, which could be why nothing is showing as it is hard to spot the mistakes. For example you are not using a construct and so setting the properties outside the object itself, with little reason for doing so (many of the properties are coming from static variables).

 

Is your code used to display different pages depending on what is set in the get?

Link to comment
Share on other sites

Hi again, I have included some code to give you an idea how to implement some of the magic methods PHP has. You should also look into __get and __set, I have implemented __construct and __tostring for you in this example.

 

Your original problem was that you was overriding the $this->temp_string when any method added to it. This is also resolved in the code below, instead of using "=" use ".=" (it appends to string variables), just remember to clear it when it needs to be reset (I would recommend making the methods return instead of caching to a $this->temp_string and then join the various returns in the __tostring method):

 

class template
{
protected $template;
protected $temp_string;
protected $prefix;
protected $suffix;
protected $items;
public function __construct()
{
	$this->template = dirname(__FILE__) . '\\templates\\';

	if($_GET['page'] !== null)
	{
		$this->template.= $_GET['page'];
	}
	else
	{
		$this->template.='index';
	}
	$this->template.= '.html';
	$this->temp_string;
	$this->prefix = '{';
	$this->suffix = '}';
	$this->items = array();

	if($_GET['page'] == 'login.html')
	{
		$this->items['TITLE'] = 'Login';
		$this->items['T_USERNAME'] = 'Username:';
		$this->items['T_PASSWORD'] = 'Password:';
		$this->items['B_LOGIN'] = 'Login';
	}
}
public function __tostring()
{

	//loading the file 
	$this->load_file(); 

	//setting the variables 
	$this->replace_items(); 

	//outputting the newly parsed template 
	return $this->temp_string; 
}
public function set_template($filename)
{
	$this->template = $filename; 
}
public function page($name, $value)
{
	$this->items[$name] = $value; 
}
public function load_file()
{	
	$this->temp_string.= file_get_contents($this->template);
}
public function replace_items()
{

	//The foreach loop is very useful when we want to loop through
	//associative arrays. 
	foreach($this->items as $assoc => $value)
	{
		//appending the variable prefixes to match. ex: {VAR_NAME} 
		$name = $this->prefix.$assoc.$this->suffix; 

		//replacing every instance of the variable with it's corresponding value. 
		//so {VAR_NAME} becomes "this is my variable" 
		$this->temp_string.= str_replace($name, $value, $this->temp_string); 
	}
}
}

$get_page =new Template();
$content = $get_page;

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.