SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Montreal
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making views smarter

    In an MVC architecture, as we all know, separation of duties is essential. What I have observed in my own code and in the code of many others is a blending of view and model code. This needn't be a bad thing. However, in an effort to improve my work, I would like to get some feedback on this.

    It seems to me that much of this could be avoided by creating view objects, separate from the templates, but that would add yet another layer that I would like to avoid. I am trying to figure out if the model's interface can be made accessible enough to avoid the need for an intermediate view object.

    Below is an example that should illustrate my quandary. It is based around a [not so] hypothetical wiki.

    "How much knowledge of the model should the designer have?"

    Two possible ways of getting the current article come to mind immediately:

    1. Template pulls the current article using, $model->articles->current_article.
    2. Controller pushes the current article ($current_article) to the template.

    In the first case, the syntax is cumbersome and potentially confusing to designers. In the second case, every time the designer wants to work with a new object, the coder must write code to push it to the view. In both cases, the coder must determine the current article and make it accessible to the designer.

    Perhaps a hybrid is possible; the coder pushes the current article's identifier to the view and the designer queries the model using that id (the form the id takes is irrelevant):

    1. Template pulls the current article using, $model->articles[$current_article_id].


    I am sure that discussion will lead to further scenarios, so I will post additional discussion items as they occur.

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > ...but that would add yet another layer that I would like to avoid.

    Why?

    What particular issue(s) do you have in that you need to have a View in the first place? Can you post some script your currently using, as I get the impression you don't have any form of View at all.

  3. #3
    SitePoint Zealot Mau's Avatar
    Join Date
    Jan 2006
    Location
    California, USA
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I tried doing what you are describing in an experimental framework. I really separated everything out and broke the controller into three pieces: authentication, action logic, template logic. And then you have your templates and models.

    What I found is that while it worked great, it created an unnecessary overhead and my code was too verbose. Most of the time, too, my template logic (which I called the glue) did not need to do anything.

    I think it makes the most sense to pass the model instances to the view. This way you avoid "middle man" states of data. For instance, if you are pulling posts from a blog, first your model would fetch the posts from the DB, then iterate over it to convert it to an array, and then your template iterates over it again. That's too much iteration.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by b1ind View Post
    Controller pushes the current article ($current_article) to the template.
    A controller has no business dealing with a template. That's view responsibility.

  5. #5
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    b1ind:

    In my opinion, main page template should consist only from HTML code and block placeholders (i e. {CONTENT}, {MENU} or <?php echo $menu; ?> ) Block templates could be solved accordingly.

    So designer should have NONE knowledge of the model, but he must know names of blocks or article's components.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •