php code

separate template from code

when you do serious coding, it’s always a good idea to separate your layout design (template) from your logic code. i’m at the beginning stage rebuilding dmb. one thing i want to do very badly is to get rid of all templates in the database. every time i load a template right now it causes me a query. i want to store my templates in files because file_get_contents is less expensive than a query. and i just figured out a very easy way to do that.

store your template in a file. it can be html, text or php, whatever suits your need.

put this in your html file, name it “test.html”

this is so $test

and then use the code below:

<?php 
$test = "cool"; 
$meep = file_get_contents("test.html"); 
eval("\$meep = \"$meep\";"); 
echo $meep; 
?>

and it will print out “this is so cool” ^_^

11 thoughts on “separate template from code

      1. mmm.. well, if you want ur programs to be scalable. It would be good for you to bet your decisions on a better piece of polished software than writing your own.. lol.. though, i seriously think that there is no point in seperating html from php, i’d rather be using CSS for controling presentation :D.. hehe, just a thought πŸ˜€

  1. I’ve always been quite curious to real benchmarks when it comes to calling from a static file vs. mySQL. With calling from mySQL, isn’t the primary cost in opening the mySQL connection? If so, then if you’re already hitting mySQL, wouldn’t it be better to simply pull everything from there?

    Do you know for sure? But great stuff πŸ™‚ I’m always too scared to use eval() πŸ˜‰

    1. well depending on what you are pulling out of the database, if you are pulling out a string that’s over 100 bytes, it definitely slows down your performance. same reason why you shouldn’t always use

      SELECT *
      

      when you don’t have to. it’s just a lot of data to handle.

      on the other hand, eval isn’t the best function to use either since it doesn’t cache. i will probably do a mixture of template functions and eval. use eval only on big overall template that doesn’t need to be cached.

      1. Interesting. The reason I bring this up is because I’ve actually experimented with caching the templating data in a filesystem; right now all templates are stored in the mySQL database directly.

        I remember when I ran ‘ab’ (apache benchmark) from the command line, there was almost no difference between grabbing from the mySQL and grabbing from the filesystem – the only problem [I think] was that grabbing from mySQL incurred an additional memory cost and extra queries.

        But the cost of opening a connection seemed almost minimal for me, so I scrapped plans on keeping templates stored in a flat file and instead just keep it all stored in mySQL.

        I’m curious about eval, but it also seems that loading a template file into memory is actually going to cost MORE than simply storing the template in mySQL. I heard somewhere that the fopen() is really cpu-intensive for PHP [which is why everyone recommends using Perl for flat-file processing] whereas getting that template data into memory from mySQL was a lot more efficient.

        So it seems if you actually need to process the template for variables, and you’re already opening a mySQL connection, it might actually be faster to keep the template itself into the mySQl database. I don’t know if you have different experiences than me in this though.

        Oh the joys of web programming πŸ™‚

        1. well my understanding is if you could avoid a query, avoid it. but i haven’t really compared query cost vs. fopen. but i think file_get_contents may perform better than fopen

          file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
          source

          maybe you are right. i’m just hearing different opinions from different people. now i heard all of thsee, i think i might stick just pure functions. in my case since most of my templates are very fixed (only colors change) i will probably just use template functions to generate the layout. have you looked at invision board’s way of implementing templates? i think it’s a bit tendious but a good idea. leslie wrote a program to test how fast a website load, invision board seems has always performed better in loading speed than other scripts. i’m quite impressed.

    2. i believe you should have opened a “persistant” connection to your database, it just opens a set of connections and everyone can use it, so never have to close it, that way, it wouldn’t tax your database in opening nad closing connections. Another advantage database have over flatfile is that databases are designed to handle HUGE amounts of infomation (i believe in theory, mysql can handle billion and billions of entries, and can in practice handle 3 million entry very efficiently :D).. mmm.. operating system is designed to allow relatively few people access stuff pretty quickly, not managing huge amounts of infomations πŸ˜€

      1. hrm read some comments concerning it and asked a few coder friends around and decide it is not worth it with all the potential problem it causes. i did try it on my board but it doesn’t seem like it changed anything.

        “Normally you do NOT want to use mysql_pconnect. This function is designed for environments which have a high overhead to connecting to the database. In a typical MySQL / Apache / PHP environment, Apache will create many child processes which lie in idle waiting for a web request to be assigned to them. Each of these child processes will open and hold its own MySQL connection. So if you have a MySQL server which has a limit of 50 connections, but Apache keeps more than 50 child processes running, each of these child processes can hold a connection to your MySQL server, even while they are idle (idle httpd child processes don’t lend their MySQL connection to other httpd children, they hold their own). So even if you only have a few pages which actually connect to MySQL on a busy site, you can run out of connections, with all of them not actually being used.

        In general use mysql_connect() for connecting to MySQL unless that connection takes a long time to establish.”

        1. hehe, i have never used mysql_pconnect. Though i have never explicitly closed any connections either :|, i just open a connection and let it be, since i see no point opening and closing all the time (i have a class for all of the general database access functions, so only need to open database in that class..).. but yah, persistant connection is useful only if you have a really popular site and many people connects at the same time πŸ˜€ i have learnt something new, thanks alot πŸ˜€

  2. But, as soon as you have over 50 pages, you’ll soon learn that your PHP/HTML system is not scaling appropriately. Especially after you have to make changes across all 50 pages. There has to be some sort of templating. Whether that entails to having:
    require(‘functions’);
    require(‘header’);
    // PAGE CONTENT here
    require(‘footer’);

    Or actually creating a true system.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s