Re: Real world usage of IT [template] class?

From: Date: Mon, 08 Apr 2002 17:58:06 +0000
Subject: Re: Real world usage of IT [template] class?
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-1107@lists.php.net to get a copy of this message
On Monday 08 April 2002 17:46, derek fong wrote: > ..... > <html> > <body> > {CONTENT} > </body> > </html> > ..... now i want to pass on my 2cents too :-), because i was also facing this problem a while ago and i was experimenting for about 2 years until i had found the right structure which fits for every application (at least those i have done until now) what i wanted for an application was: - seperate presentation and programming-code - have a clear directory structure - provide easy and page-wide error handling easily - switch designs on the fly, also using frames but never change the php-code. use a css-files for each design too - make it easy to translate the entire web site - process actual action before showing anything on the page so the page can be build depending on the result of the action taken place, i dont want to process half the page and then an error occurs and i have to write an error somewhere in the middle of the page things i do/use anyway [in brackets the directory-name] - only classes contain the (business) logic [classes] - split the application in modules [modules/modulesName] - different layouts for the same application [layouts] i dont know if this would also work using IT[X] as the template engine, but anyway. so here is how i handle this: [...1...] main layout to handle the layout i created a main-layout. since every webpage more or less looks like this: ---------------------------------------------- headline/header ---------------------------------------------- navi | navi | navi | content navi | navi | ---------------------------------------------- footer ---------------------------------------------- where those elemetns resides can be determined in this main-layout, which actually is only a template, that contains one (or x) table(s) which include each of those elements. i.e. for the above sketch it would be: ( resides in: /layouts/<current layout>/main.tpl ) <table> <tr> <td>headline/header</td> </tr> <tr> <td>navi</td> <td>content</td> </tr> </table> where each element inside a 'td' is only meant as a space holder here [...2...] layout class the layout class handles the content of each element, say which php-page shall be shown, i.e. which php-page is the navi element, etc. it implements get/set methods for each element, and you can use/add elements as many/litttle as u want $layout->setNavigationFile( 'path/to/filename.php' ) $layout->getNavigationFile() $layout->setCurrentTemplate( 'path/to/filename.php' ) $layout->getCurrentTemplate( ) see: http://wolfram.kriesing.de/libs/php/docs/myPEAR_Page_Layout.html for a list of the methods, its not well prepared for PHPDoc, but u can see a list there by having a single php-file for each element it can handle itself and the content it shall show. this is very helpful for a tree-navigation i.e. which unfolds leaves depending on the page currently shown, you only include the navi and it handles itself! [...3...] include order, building the page the actually called page is the only one you really put src-code in. if u call http://..../news/index.php in the browser the "news/index.php" also contains the code for showing the news, or at least processing the data and preparing it for the template, to let it do the showing. so the "news/index.php" only includes a php-file at the beginning and one at the end, you could also handle this via auto-pre- and append. the inclusion works like this, for a non-framed site { /config.php (0) /modules/news/index.php (1) { /finalize.php (2) { /layouts/main.php (3) { /layouts/<current layout>/main.tpl (4) { header.php { header.tpl } (5) navi.php { navi.tpl } (6) /layouts/<current layout>/modules/news/index.tpl (7) footer.php { footer.tpl } (8) } } } } } (0) - includes all commonly used stuff, i.e. the $layout object, directory defines, etc. (1) - the actual page called by the browser, processes the data received via $_REQUEST, if given and prepares the data that the template shall show the errors are caught here and can be handled as needed and no output had been generated yet, so even a redirect to a global error page is possible (2) - handles the proper inclusion and determines the necessary template if not given explicitly, i.e. for "/modules/news/index.php" we need "/layouts/<current layout>/modules/news/index.tpl" and sets the property in the $layout object, calling: $layout->setCurrentTemplate( '....' ); (3) - simply includes the "main.tpl" (4) - the main template as mentioned further above under [...1...] (5)(6)(8) - see [...1...] (7) - the HTML-template that shows the data generated in the php-file in (1) using this structure you can change the design anytime, to anything else and the designers only have to touch only the templates, which reside in "/layouts/<layout-dir>" and since there is always a fallback-layout (i usually have "default") you can also start designing page by page and every template that is not created in the current layout-dir is taken from the default-layout, this way the application is always running and you can add designs on the fly without changing any php-file [...4...] processing page and content presentation as mentioned before the code and the presentation layer are seperated completely. the page "news/index.php" processes/generates the data. this is done only by calling methods of the existing classes, sometimes a page is as small as this: $object->save( $_REQUEST['saveData'] ); and the template only shows the page and uses the php-values set in the php-file, doesnt process anything itself only loops and stuff [...5...] template engine i was starting with IT[X] switching to smarty then and finally doing my own, see below for a link. but this has a lot of advantages for me, at least, since smarty was to unflexible and IT[X] even worse, in my eyes the most important thing was that i wanted a solution that allows also to translate pages easily, and this is what finally came out, for example see: http://wolfram.kriesing.de/libs/php/SimpleTemplate/examples/fullFeatured/ klick on the language-links (german/english) to see the translated page. part of the translation thing is also integrated in PEAR I18N this way i can easily copy/paste an "appl_template" directory, which contains a default setup of an application and i can start coding the real stuff and i dont have to start every time with building a "appl-framework". if anyone is interested in more info or in receiving a copy of the "appl_template" to start trying it mail me. i will also try to put it online when i find the time. for further interest/questions simply mail me. some online-resources: [ 1 ] for german readers a short summary why i needed my own template language http://www.list.kbx7.de/list?sub=973596&id=176767010 [ 2 ] or directly, the template engine http://sourceforge.net/projects/simpletpl [ 3 ] error/message handling class(es) http://wolfram.kriesing.de/libs/php/docs/myPEAR_Page_Message.html http://wolfram.kriesing.de/libs/php/docs/myPEAR_Page_Error.html [ 4 ] all PHPDoc's http://wolfram.kriesing.de/libs/php/docs/index2.html [ 5 ] the authentication system i use, alike tomcat (catalina) http://sourceforge/net/projects/auth good luck -- Wolfram ... translating template system ... http://sourceforge.net/projects/simpletpl

« previous php.pear.general (#1107) next »