[Phplib-users] template extensions
Brought to you by:
nhruby,
richardarcher
From: Evan H. <hu...@bi...> - 2003-01-14 19:28:47
|
Andrew Crawford wrote: ... > Maybe I'm not understanding something. That just seems backwards to > me. Normally, the PHP file is being evaluated and pulls formatting > information from the template. So, now, the template would be evaluated > and pull code from another file? Why? To abstract the presentation away from the code. Currently, if I want to add a dynamic block of html (one that's backed by code instead of static data from a file), I have to: 1. write a bunch of code to create the dynamic content 2. modify the php script that will be including the template (to add the set_var()) 3. modify the template to include the named variable In a situation where variables are backed by functions, step two can be dropped. This lessens the coupling between the php script in 2 and the template in 3. I'll be the first to admit that this isn't a huge saving. All it gives us is: a) Faster runtime (there's no risk of unused variables being created) b) Enforced encapsulation of variable definition into separate files. Notice that this really encourages reuse. c) Greater decoupling of templates and their defining code. But it comes at virtually no cost. The only change to the API that I see would be a "set_backing_dir(path)" which would set the path to the directory that contains the files that would back the variables. As a side note: Consider the implications of (b) and (c) from above. If we take the whole variable backing thing to an extreme, we can move all of the dynamically generated content out of php scripts and stick them into files. This means we don't need any php between the templates and the outside world. If we use .htaccess to prevent download of .tpl files, and catch requests for them with an error_document handler (which would evaluate the named template), we can essentially provide an interface where we're just serving raw templates. > Instead of putting those functions in the template, couldn't you just put > template variables in the template and have the PHP code filling in the > template perform the functions for those variables? You could. What Mike and I are suggesting is essentially just a lazy linker: instead of exhaustively defining all possible variables before the script is run (or worse, sculpting code that will conform to the templates), simply move the resolution of variables to a just-in-time model where they're defined as needed. > Or, if there is some reason I am missing to want to do this, why use > templates at all? Why not just use straight PHP and use <?php include > your_function.php; ?> or <?php your_function(n); ?> in place of your > curly-bracketed functions? Because that mixes presentation with logic. As soon as it's in vanilla php, we lose the ability to use templates (unless we're calling out to other files, etc). -- Evan Hughes OpenConcept Consulting (openconcept.ca) |