Archadian Posted October 6, 2008 Share Posted October 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127276-contents-not-getting-passed/ Share on other sites More sharing options...
keeB Posted October 6, 2008 Share Posted October 6, 2008 Your class name is 'template' not Template. Quote Link to comment https://forums.phpfreaks.com/topic/127276-contents-not-getting-passed/#findComment-658293 Share on other sites More sharing options...
Archadian Posted October 6, 2008 Author Share Posted October 6, 2008 I changed it but still nothing. Thanks for the reply Quote Link to comment https://forums.phpfreaks.com/topic/127276-contents-not-getting-passed/#findComment-658393 Share on other sites More sharing options...
Archadian Posted October 6, 2008 Author Share Posted October 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127276-contents-not-getting-passed/#findComment-658465 Share on other sites More sharing options...
jcrocker Posted October 7, 2008 Share Posted October 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/127276-contents-not-getting-passed/#findComment-658819 Share on other sites More sharing options...
jcrocker Posted October 7, 2008 Share Posted October 7, 2008 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; Quote Link to comment https://forums.phpfreaks.com/topic/127276-contents-not-getting-passed/#findComment-658838 Share on other sites More sharing options...
Archadian Posted October 7, 2008 Author Share Posted October 7, 2008 nice. thx for the reply and the help Quote Link to comment https://forums.phpfreaks.com/topic/127276-contents-not-getting-passed/#findComment-659130 Share on other sites More sharing options...
nadeemshafi9 Posted October 10, 2008 Share Posted October 10, 2008 well u need to identify the problamatic parts and only show them this is to vague, it takes more than 10 mins to understand thats your job our job Quote Link to comment https://forums.phpfreaks.com/topic/127276-contents-not-getting-passed/#findComment-661918 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.