RE: [Phplib-users] Template: How to handle conditional html?
Brought to you by:
nhruby,
richardarcher
From: Brian P. <bp...@ct...> - 2001-11-26 17:03:46
|
>>"I am just learning PHP and I would like to know what reasons you have >>for that statement." There's been a lot of discussion about it here on this forum, but my primary reason for saying that is just personal experience. I built a system when I first started using PHP. It was fairly complicated(100+ pages) and to be honest it has worked very well. In that system, almost all the PHP is being generated dynamically in the PHP. Unfortunately, this system has been a beast to maintain. When I need to make a very simple cosmetic change, I have to dig through pages and pages of complicated PHP code. Likewise, when I need to make a change, even a simple change, to my code, the PHP is needlessly cluttered with lines and lines of echo/print statements. My new way, which works infinitely better, is to use templates almost exclusively. I create two files for every page, one called xxxx.php and one called xxxx.html. xxxx.php does all the thinking and xxxx.html just sits there looking pretty. xxxx.php will normally look something like: -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= include "../globals.php"; my_page_open(); include ABSOLUTE_ROOT . "header.php"; $t = new Template; $t->set_file ( "main", "xxxx.html" ); $t->set_var ( "CURRENT_DATE", date ( 'm.d.y h:i:s' ) ); ... $t->parse ( "out", "main" ); // (writes the xxxx.html file to the browser) include ABSOLUTE_ROOT . "footer.php"; my_page_close(); -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= So if I decide I want to change the date format, it's fairly easy to find it in the xxxx.php file. Or if I decide I want to make the date fluorescent pink, I can just pull up the xxxx.html file in Dreamweaver and modify the formatting on the {CURRENT_DATE} tag. This is admittedly a very simple example; the real power of templates comes when you start working with database data, etc. Give templates a try. Once you get comfortable with the limitations (namely dynamic HTML), I think you'll love how much more maintainable your code becomes. -----Original Message----- From: Menzi, Hans [mailto:HM...@ec...] Sent: Monday, November 26, 2001 10:24 AM To: Brian Popp Subject: RE: [Phplib-users] Template: How to handle conditional html? "It's normally not a good idea to put HTML in your PHP" I am just learning PHP and I would like to know what reasons you have for that statement. -Thanks Hans Menzi -----Original Message----- From: Brian Popp [mailto:bp...@ct...] Sent: Monday, November 26, 2001 11:03 AM To: 'Peter Holm'; Phplib (E-mail) Subject: RE: [Phplib-users] Template: How to handle conditional html? True. It's normally not a good idea to put HTML in your PHP, but I think there's a time and a place. With a very dymamic menu, for example, I'll usually put a single tag {menu}, and then build the HTML like so: function addMenuItem ( $label, $link, $active=false ) { $html = "<td>"; $html .= ($active ? "<B>" : "" ); $html .= $label; $html .= ($active ? "</B>" : "" ); $html .= "</td>"; return $html; } $html = addMenuItem ( "Home", "home/", $currentAction=="home" ); $html .= addMenuItem ( "Links", "links/", $currentAction=="links" ); $html .= addMenuItem ( "News", "news/", $currentAction=="news" ); $t->set_var ( "menu", $html ); (I find that if you use functions, the PHP stays very maintainable and readable. ) -----Original Message----- From: Peter Holm [mailto:PH...@gm...] Sent: Saturday, November 24, 2001 10:37 AM To: Php...@li... Subject: [Phplib-users] Template: How to handle conditional html? Hi, maybe some of you came across this problem also: there are somtimes elements of a site, that should be shown only under certain circumstances, e.g. you have a browsing page with "previous" and "next" buttons but you don´t wnat the 'previous" button to be shown if there is nothing before the actual shown item, vice versa you want to hide the "next"-button, if there is nothing next to show. Maybe you have different pictures for this situation and maybe, to make it even more complicated, someone wants you to have a rollover only on the "active" next/previuos buttons. OK, this would be easy usually with some if-statements in the code, but when using templates, this breaks the sense of them somehow. If you use something like {NEXT} in your template-file which contains the whole html for that link ('a href' and img), there is nothing to see in the template anymore (if someone is generating it with an graphic al tool). Otherwise it is not good to use some php-code in the template-file, so I tend to the first solution, what makes some more work... Does anybody have any other ideas about that? Thanks for your attention, Peter Have a nice thread, Peter _______________________________________________ Phplib-users mailing list Php...@li... https://lists.sourceforge.net/lists/listinfo/phplib-users _______________________________________________ Phplib-users mailing list Php...@li... https://lists.sourceforge.net/lists/listinfo/phplib-users |