I have been writing PHP for about 2 years now, and finally have the time to actually experiment with different ways to seperate HTML from actual PHP code.

I have been reading many articles on this subject, and have even examined source of comercially available software.

I am wondering if there is not some fundamental idea or concept that I can not get a grasp on, becaue it seems to me that it is most in efficeint to have a PHP parser examine HTML code.

Please let me know what methods you use, and how they compare in effecientcy and maintainability, to other methods you have tried, or previously used.

Thanks

PHPdev

    Best way to separate PHP from HTML is to use a template system. I personnaly use PHPLIB, but I know there are numerous other classes designed to do just that.

      If you want to separate your HTML from your PHP you are going to run into the efficiency problem. Yes having PHP parse your HTML is less effiecient than just having the HTML interspersed within your PHP scripts.

      Now that being said it is much easier to maintain your PHP code when you aren't trying to deal with all of the HTML tags. My first attempts at separating PHP from HTML was to just use print() and echo() statements to output all of my HTML so all I was looking at was PHP code. This worked well except for when people decided that they wanted to re-design the pages. Now I had to go through all of that PHP and find the proper print statements and change them.

      Using a Templateing system like PHPLIB (which I've never used, always grew my own) is the best solution. You can give the HTML designers some basic rules to follow, what macros to use and the like, and all you have to worry about is making the code work. As long as you are putting the proper data into the proper macros then the HTML can change in any way it wants and you never have to concern yourself with it.

        eoghain,

        I am interested in learning more about the templating system you have implemented. I am not big on using packaged library routines, some say I may be a controll freak.

        What I am most interested in is how you are storing the templates, whether in a file, or database. How you parse the HTML, and replace the appropriate stings, with values.

        I am currently experiencing many of the same issues with redesigning sites, or page layouts, because I too have been using the echo() methodology.

        Thanks

        PHPdev

          My approach is to implement whatever templates I need.
          But I always have in mind that it should be modular and easy to expand.
          Also be sure to make it easy for designers to use.

          So if you wanna make your own thing.
          Define your needs ... design it with the future in mind ... and have fun coding. I think its a blast.

            Currently I've been working with HTML files for each type of object that I'd be displaying on my screen. So for a table I'd have these files:

            table.html
            <table name="basic_table" class="basic_table">
            #DATA#
            </table>
            
            table_header.html
            <tr class="table-header">
               #TABLE_HEADER#
            </tr>
            
            table_column_header_row.html
            <tr class="column-header-row">
               #COLUMN_HEADER_ROW#
            </tr>
            
            table_column_header_cell.html
            <td class="column-header-cell" nowrap>#COLUMN_HEADER_CELL#</td>
            
            table_cell.html
            <td class="cell">#CELL#</td>
            

            As you can see I use #NAME# as my macro this is what I'll replace in my code. I'm also using CSS to handle all of my display values (color, font, etc...). Now I take these files and load them into memory and then regex the strings to build by HTML for output. This gives me the ability to build a table with as many rows/cols as I want. Once I have this table built I put it in the place of another macro that is sitting on my main page layout.

            This is just something that I've been playing with and I haven't looked into the speed of it yet, but it should give you an idea of how to implement a system on your own. These files could easily be held in a database and selected when needed. I'd suggest that you look at the PHPLIB code to see what they are doing, I plan on doing that next to see if there are any tricks I can learn.

              eoghain,

              Thanks for the response. I guess I have been making things to complicated, and trying to figure out a way to do the actual replacing in the html code.

              I know one article I was reading it appeared as if they actualy parsed every character of the HTML file looking for the comment.

              Thanks Again

              PHPdev

                Write a Reply...