Page is a not externally linkable
incrediBILL - 3:19 am on Nov 15, 2011 (gmt 0)
I actually have quite a bit of logic in the code besides that 3 line snippet eval() example, it's an entire class actually. I was just getting tired of building the class and looking for a replacement if something was suitable and I'm not finding exactly what I want so maybe I'll just keep working on my class library.
I prefer to parse the files line by line to sub out the variable chunks.
After reading the entire file in, I explode each printable area into an array using the special comments that tag each printable area of the page such as...
"<!-- tagname="menu" --!>"
So my function PrintTemplateTag() returns all the HTML between the specified tagname in the template file and the next tagname, or end of file, whichever comes first. A default template tag "HEADER" prints from the beginning of the HTML page to the first tag encountered, so printing a page using my templates, with a loop in it for repeating data looks kinda like this:
PrintTemplateTag("header");
PrintTemplateTag("body");
for( x=1; x<11; x++; ) {
$list_item="This is line #" . x;
PrintTemplateTag("list");
}
PrintTemplateTag("footer");
The reason I use actual comments is because the browsers and developer studio tools all work with them and handle them just fine, and they can be hidden and unhidden as needed without mucking up the template with a bunch of visible junk.
What if I want to conditionally format the tables?
The point being, there is no simple, clean way to do this.
Regarding tables, I print them from within the template.
Here's how I do a table for instance...
the HTML looks like this: <!-- tagname="itemlist --!>
<table>
<tr><td>$title</td></tr>
<tr><td>$rowdata</td></tr>
</table>
My class that prints stuff can call out each row of a table by line number or if you want to insert comments between the row, by name. When the PrintTagnameTable() function is first called it extracts the entire table into a buffer and explodes the rows into an array, therefore repeated printing of rows is very fast, thus giving the same working functionality without putting tables in separate files.
The code to print a table looks like this: PrintTagnameTable("itemlist",table_start);
$title="whatever";
PrintTagnameTable("itemlist",1);
for( x=1; x<11; x++; ) {
$rowdata="This is data row " . $x;
PrintTagnameTable("itemlist",2);
PrintTagnameTable("itemlist",table_end);
The table_start prints everything from the tagname preceding the <table> to the first row <tr> and the table_end prints from the everything from the last </tr> including the </table> to the next tagname, but I'm contemplating making it just print the end of the table only without continuing to the next tagname, who knows.
What do you think of that method?
So other than a few comments and a couple of variables in the pages, this stuff looks very good in a WYSIWYG interface or just previewing in a browser for that matter, real close the the final web page display opposed to Smarty or such, which is what I'm after, actually seeing the design and not all the code when designing.
Being fundamentally lazy ;) I was hoping someone had done something closer to what I want besides Smarty, or some of the other heavy frameworks, but it's sounding like I need to just toss the use of eval() to avoid errors and do my own variable replacement code and call it a day.
I also use this class for creating email templates, plain text or HTML, the code really doesn't care if it has any HTML in it except for the tagname comments, so using it to create an email template actually works really well too.
I really don't want to write the code, I just want to use the code, but I can't find what I really want.
It's like the people building PHP only focus on what the programmers want and really don't give that much thought into the fact that ultimately it's a web page, and being able to integrate with templates should be an integral part of the language and not left up to a bunch of 3rd party template libraries, getting us all on the same page (pun intended).
Damned annoying.