Jump to content

robgolding63

Members
  • Posts

    20
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

robgolding63's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. All is well now, turns out I had missed out a fullstop when assigning variables to the template Thanks for all the help, I've almost finished moving the whole site over to the template system, and it's much neater now - but it's a little bit slower than before - takes about 0.04 seconds to parse a page - is that average? Rob
  2. This is exactly how I am doing it, but with "article" as the name of the constructor, not __constructor - as a function with the same name as the class is also considered a constructor, is that correct? The strange thing is, everything is working, apart from the looping part of the template engine - it loops over the code the correct number of times, but doesn't replace the variables with anything, so I end up with {related.NAME} instead of the name of a related article . The fact that only one function doesn't work correctly suggests that it's my fault, but it works fine when called from a normal variable within a class, ie. $template = new template("article_body.html"); instead of $this->article = new template("article.html"); If you want I can post the code for the template engine, and the article system as well, at the moment I am working on http://maxms.net/test.php Thanks for the help, I'll look over the code to see if I can find any errors, but I don't even know where to start! Rob
  3. Actually I'm running PHP5, is it very different? I thought you had to use "var" . Everything is working with the templates, it replaces variables and outputs correctly, but just wont loop over block variables, with the assign_block_vars() function. Thanks for the help, Rob
  4. Hi, I have written a template system, which is called like so: <?php $template = new template("index_body.html"); $template->assign_var("TITLE", "Site Title"); $template->output(); ?> What I want to ask is, is it supported to instatiate the template class (or any other class for that matter) from inside another class. I am rewriting the article system on my site, and for the viewarticle function, I obviously need to instatiate the template class, pass it some variables, then output the compiled template. I am doing this as follows: <?php class article { var $template; $this->template = new template("article_body.html"); $this->template->output(); } ?> Is this correct? The reason I am asking is because the assign_block_vars() function won't do what it's supposed to - (similar names and functions to phpBB, but code is much simpler).
  5. Well, I finished it today I don't know how clear or "proper" my code it, but it's my first attempt at a neat, object-oriented project, so here's the main template code: <?php class template { var $page; var $template_path = "templates/"; var $vars; var $tpl_data; var $cur_loop; /* /* Constructor */ function template($template) { // Check if template file exists if (file_exists($this->template_path . $template)) { // Load contents into $this->page if (!$this->page = file_get_contents($this->template_path . $template)) { die ("Error loading template"); } } else { die("Could not find template file"); } } function assign_var($varname, $varval) { $this->vars[$varname] = $varval; return true; } function assign_vars($vararray) { foreach ($vararray as $key => $val) { $this->vars[$key] = $val; } return true; } function assign_block_vars($blockname, $vararray) { // Add a new iteration to this block with the variable assignments we were given. $this->tpl_data[$blockname][] = $vararray; } function loops() { foreach($this->tpl_data as $loop => $vararray) { $this->cur_loop = $this->tpl_data[$loop]; $this->page = preg_replace_callback('^<!--\s?LOOP\s('.$loop.')\s?-->(.+)<!--\s?END\sLOOP\s?-->^si', array('template', 'loop_callback'), $this->page); } } function loop_callback($matches) { $result = ''; foreach ($this->cur_loop as $vararray) { foreach ($vararray as $key => $val) { $result .= str_replace("{".$key."}", $val, $matches[2]); } } return $result; } function compile() { $this->loops(); foreach ($this->vars as $key => $val) { $this->page = str_replace("{".$key."}", $val, $this->page); } } function output() { $this->compile(); print $this->page; } } ?> It would be great if you had any tips or warnings about things that I may have done wrong, I need to make sure the code is OK before putting it into production Thanks for the help, Rob
  6. My only other idea for this would be to cheat, and have the PHP construct the loop into a variable, and then pass that to the template so all it would need to do it display the contents. ie. <?php $menu = ''; while ($row = mysql_fetch_array($sql)) { $menu .="<a href='".$row['href']."'>".$row['title']."</a><br />"; } ?> But that's not very neat at all, and I can't figure out any other way to solve the problem, is there an elegant solution that is simpler, or would my template system have to become very complicated to handle such functions. Thanks for all the help, Rob P.S. I will have a good look through that code, frost110, thanks for posting it!
  7. Well, this is more of a fun project than anything, I want to be able to maintain my own code, and know exactly how everything works. I much prefer running my own code, but if this becomes a huge project, then I will consider using ETS. If anyone knows a way to have a preg_replace() or similar function loop over a string for a given array (ie. in a foreach or something?), then I would be extremely grateful. Thanks for the reply, Rob
  8. Hi, I am writing a simple template engine for my site, to make changes to layout and styling easier. At present, it's basically just an object-oriented find-and-replace script ie. replace {PAGE_TITLE} with the title of the page. I am stuck at the point of looping variables/database rows. How would one pass an array to the template, or make the template loop over multiple database rows, to output news items or menu items etc. I have looked into template engines already out there like Smarty, but I would like something fast and simple, as my site isn't very large yet, and I want to keep load times as low as I can (I will be writing a caching system once the engine is completed). Thanks for any help, Rob
  9. OK so would making a new object basically connect again if I did that inside the class? If so, how do I pass the object via the construct? Rob
  10. Well I am wondering how other people have accessed the mysqli class from within other classes (it works if I do it from outside another class). Thanks, Rob
  11. Hi, I want to start using mysqli on my site instead of mysql, to take advantage of prepared statements. In my mysql.php file, which holds the connect functions for mysql, I added: $mysqli = new mysqli("localhost", "user", "pass", "db"); so now there is a class called mysqli, that I use to carry out DB operations (I hope that's how it works anyway). But when I am inside one of my own classes, I cannot access $mysqli to do queries and such like. I get this error: What simple thing have I missed? Thanks for any help Rob
  12. Any ideas? I thought about having a PHP file load the first 100 characters into a summary field in the DB, and checking the file every so often to see if it has changed. The only problem I would have is how to get a PHP file to run on a schedule, on a windows server (can't do a CRON job). Thanks, Rob
  13. I have all this working nicely, and I should be all sured-up against SQL-injection, but I wanted to ask about performance. When I print a list of articles, in a category for example, the PHP has to get the first 100 characters from each HTML file. Is this a big perfomance hit? I can post the code if it would help, but basically it gets the contents of each file, then uses substr() to get the first 100 characters. It has to do this for every article being printed out in the list, so it's like a summary for each one. Thanks, Rob
  14. OK it's all working already! I just copied all the sources from the DB into HTML files, and changed the articles class to include it instead. Also, I made my own getcontents() funtion, so that if the file doesn't exist for some reason, I dont get an ugly error! If you're interested I can post the code. The site is www.maxms.net - I only have 7 articles so far, so it was quick to transfer all the sources over. This'll make it much easier to edit them. Thanks! Rob
  15. The problem with the htmlentities this is, I actually need it to still be in parseable HTML format, so that when it is fetched from the database, it is parsed properly. In reply to shaun, that sounds like a good plan! If I store the details about the article in the database, with the name of a HTML file that contains the content, I can just include that - hows that for a plan? Thanks guys, Rob
×
×
  • 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.