Re: smarty-general Digest 13 Mar 2003 13:54:55 -0000 Issue 213

From: Date: Thu, 13 Mar 2003 15:22:56 +0000
Subject: Re: smarty-general Digest 13 Mar 2003 13:54:55 -0000 Issue 213
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-8620@lists.php.net to get a copy of this message
At 08:54 AM 3/13/03, you wrote:
Hmmm....but the calling script most often contains business logic, correct? That would place it in the conceptual "Model", and not the controller. Ah well, I'll have to think about this a bit more. Any additional input is welcome. Thanks! John
When you first read about a concept like MVC, it makes perfect sense. As soon as you go to implement a solution, however, you realize how messy and confusing it can be to apply theory to practice. There will always be compromises, and a certain amount of leakage between the Model, View, and Controller. Some code is clearly within one of the three, while other bits of code could arguably be placed in either the View or Controller sphere, or in either the Model or Controller sphere. The one thing you can know for sure is that "View" logic and "Model" logic should never creep into each other's spheres. For an example, think of an ecommerce web site. For historical reasons, this site creates a file on the hard drive to store the contents of each 'shopping cart.' There are Model functions to read the files and return an abstract data structure, and to write out abstract data structures as files. There are Control functions to decide whether a new 'cart' is needed, or whether an old one should be looked up. There are View functions to create an html representation of the contents of the 'cart.' In an ideal world, the Control functions have no knowledge of where the 'shopping cart' data lives. If Control code needs to know whether a certain user already has a shopping basket or not, it should not parse a directory on the hard drive to do so. It should call a Model function like 'get_user_shopping_cart()'. That Model function would examine the hard drive, and either return a data structure or FALSE. Furthermore, the Model function would not automatically create a 'cart' file -- it would be up to the Control code to call 'get_new_shopping_cart()'. This way, the 'carts' could someday be stored in a database, or changed from comma delimited files to XML, or whatever you wanted, and the Control would stay the same. Similarily, View functions would not impinge upon the Control code's domain. Ideally, there will be at least two View functions that would display a 'cart': one for the user placing the order, and one for the administrator examining a completed order. You would never write a View function that displayed both, and decided which to choose. The Control code would either call 'generate_user_shopping_cart()' or 'generate_admin_shopping_cart()'. I see Smarty firmly in the View domain. Control code typically lives directly in the php script, but may have portions pulled out and put into functions for either readability or re-use. Model code deals with external entities like the filesystem and database servers. My best practice is to have three separate files, one for each of Model, View, and Controller. Keeping them physically separate is helpful conceptually, and also promotes code re-use. I now have a standard library that can be dropped-in to a project. Sticking with MVC seems like extra work at first, but will pay off in the long run. For example, I was able to recently replace a View class for a large web project with Smarty. It was practically plug and play.
Jason Sweat said:
Would Smarty be considered the controller?
My answer to that questions would be no. Smarty is firmly in the "View" part of the model view controller paradigm. If anything, the php script calling the smarty object could be considered the controller, particularly if the script selects different data, and shows different templates based on context. ----- Original Message ----- From: "John Wells" <jb@sourceillustrated.com>
Thanks for the info. I've seen both phrame and php.mvc. I guess my question if more general and specific to Smarty's common usage. In other words, would your typical app developed with Smarty be considered MVC? Would Smarty be considered the controller? Thanks, John
-- Mike van Lammeren Web Application Developer Emerge2 Digital Inc., "evolving business to digital" Web: http://www.emerge2.com Email: mike@emerge2.com

« previous php.smarty.general (#8620) next »