Re: [SMARTY] Re: include_php options / best practices
| From: | Justin Patrin | Date: | Fri, 11 Jun 2004 00:54:55 +0000 |
| Subject: | Re: [SMARTY] Re: include_php options / best practices | ||
| References: | 1 2 3 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-11444@lists.php.net to get a copy of this message | ||
Sarah Gray wrote:
Justin Patrin wrote:The second solution is the one I use on the shopping cart on this website (http://www.pnicorp.com/pniCart). It works well, and although it is annoying to have to include header and footer, it means that some of your pages have the option not to ;-). I also went further, making different controller and sub-controller objects which assign vars and templates and hand things off to their own sub-controllers so that the code for one task is in one place. But that's a bit higher level than what were talking about. ;-)I would suggest splitting your templates differently. Put the stuff above your "dynamic" template include in a header.tpl and the stuff below it in footer.tpl. Then, include header.tpl and footer.tpl in the other templates. Your PHP which does the $t->display() call should be the one choosing which template to use. The PHP that calls that should also assign the correct vars as necessary. So something like this: if($content1) { $t->display('content1.tpl'); } else { $t->display('defaultContent.tpl'); } header.tpl: This is the header footer.tpl: This is the footer content1.tpl: {include file="header.tpl"} Content 1 {include file="footer.tpl"} defaultContent.tpl: {include file="header.tpl"} Default Content {include file="footer.tpl"} If you don't like that solution, I would suggest that you still assign the vars for each page in the code that calls $t->display() (using a switch or if), then assign a special var which tells index.tpl which content template to include, then call $t->display('index.tpl'). index.tpl: header stuff {include file=$contentTemplate} footer stuffYup -- I think that last solution is the the clear-headed solution I was looking for. I was trying to get around including header.tpl and footer.tpl in each template file (trying to be economical and I have some breadcrumb-y stuff going on that I'd have to redo), so I'll try your latter solution. If it doesn't work, I'll do the first thing, which is definitely straightforward (which has been known to trump clever).
Out of curiousity, why is include_php being deprecated?I assume because it's a hack and could be emulated by using: {php} include('file.php'); {/php} or <?php include('file.php'); ?> Both of those are easily turned off and obviously are normal PHP code rather than {include_php} which looks like a normal Smarty call. BTW, you could use either of those top options to do what you originally wanted, but I would suggest not doing it for the same reason I would suggest not using include_php. They fuddle the seperation between your "controller" and "view" code. If you do some assigning and such outside *within* a template, you can get spaghetti code real fast.
Thanks, s.-- paperCrane <Justin Patrin>-- paperCrane <Justin Patrin> -- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php