SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Rome
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MVC views: how many for similar data-showing?

    Hello,
    we are developing a reservation system.
    We already implemented a model and a controller to search both car transfer or hotel rooms to reserve.
    We obviously get different listings depending on the type of service searched. What's the best way, OYO, to show the different listings:
    1. Extend base view into two per-type views. Like MyCarLIstingView and MyRoomListingView.
    2. Implement two templates to be loaded by the same view
    3. Other else

    Let me know what you'd do. Bye!
    Last edited by jakuza; Jan 19, 2006 at 18:32.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Rome
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I propend for the One View - Many Templates way to go....

  3. #3
    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)
    Do you have a class called View ? I think I would call that a controller or action. The output of your script (the HTML) is the view. (At least in my terminology).

    Whether I would have two templates or one, depends on how different the two views are. I would always have two controllers though. If they are very similar, they might extend from the same abstract baseclass. (Or one might extend the other - depending on which makes best sense)

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've worked with a design that had separate instances for each view and view component (the usual navigation/menu/content parts). This lead to an exploding number of classes and controlling logic in the view layer.
    I think it's much more flexible to have one view instance assigned to a controller that renders templates and holds all view data. I currently use what I call an HtmlView which renders PHP templates and HTML elements. All templates are rendered inside the instance and have access to all (public) instance variables and methods. It's very easy to bind data to form elements this way so you can reuse templates without akward control structures.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Rome
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uhm... 15-15
    Quite like my previous 0-0
    We need more opinions....
    Thanks in the meanwhile to you.

    @michel: your class explosion fear is mine. All this thread is addressed to that. How do you decide which template to load in your do-it-all view? I hate if's....

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jakuza
    How do you decide which template to load in your do-it-all view? I hate if's....
    Controller methods assign view variables and render templates. These are usually wrapped in a PHP template that renders a complete response body, like:
    PHP Code:
        <?=$this->dtdTag()?>
           <?=$this->htmlTag()?>
           <?=$this->head()?>
           
           <body>
               <?=$this->render('shared/nav')?>
               <div id="content">
               <?=$this->content?>
               </div>
           </body>
           </html>
    Here the variable 'content' holds template output rendered by a controller method. The controller decides when to wrap.
    Since all templates have access to View methods, they can render other templates. These are usually shared bits (login forms etc.).

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm...

    I think you've reached the point where you need to consider the Composite View... Well I would say that though, since I'm biased towards that pattern

    But I would consider it at least anyways.

  8. #8
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Do you have a class called View ? I think I would call that a controller or action. The output of your script (the HTML) is the view. (At least in my terminology).
    If you need presentation-related logic, would that be in the controller or action as well?
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  9. #9
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dagfinn
    If you need presentation-related logic, would that be in the controller or action as well?
    What kind of presentation logic doesn't fit in a template?

  10. #10
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    I think you've reached the point where you need to consider the Composite View... Well I would say that though, since I'm biased towards that pattern
    Yes, you would indeed. I meant to do a thread on nested actions / views the other day as I think I reached a rather nice and simple yet effective solution, but I couldn't put it together in a way that would've satisfied me so I left it there. Well, I'll see if I can get around to it some day.

  11. #11
    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 dagfinn
    If you need presentation-related logic, would that be in the controller or action as well?
    Either in the controller (action) or in the template. I'm having a hard time deciding between thoose two really - I think it depends on whether the logic is purely graphical (like alternating colors) or if they are more tied to the state of the application (like localizing strings, or formatting value-objects to strings). I might throw a view-helper in there for very complex views (mostly forms and form-widgets), but for most purposes I wouldn't.

  12. #12
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Either in the controller (action) or in the template. I'm having a hard time deciding between thoose two really - I think it depends on whether the logic is purely graphical (like alternating colors) or if they are more tied to the state of the application (like localizing strings, or formatting value-objects to strings). I might throw a view-helper in there for very complex views (mostly forms and form-widgets), but for most purposes I wouldn't.
    The reason I asked was that you said the View was the HTML output. If you say the template is part of the View, then it starts to make more sense.

    It obviously depends on the amount of code, but what I'm currently trying to do is to keep the View logic somewhere separate, but not wrapped around the template.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  13. #13
    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 dagfinn
    The reason I asked was that you said the View was the HTML output. If you say the template is part of the View, then it starts to make more sense.
    I guess that is bordering on being a philosophical. Couldn't we say that the template (the file) is just a resource and thus not really part of any layer (Let's ignore PHP-style templates here). Then we have the Template parser, which takes the template and binds variables to it. I'm not sure if that class should be said to be part of the controller layer or the view-layer ?

    Quote Originally Posted by dagfinn
    It obviously depends on the amount of code, but what I'm currently trying to do is to keep the View logic somewhere separate, but not wrapped around the template.
    How then ? Through callbacks to helpers (pull) or by doing the formatting in the controller (push) ?

  14. #14
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Then we have the Template parser, which takes the template and binds variables to it. I'm not sure if that class should be said to be part of the controller layer or the view-layer ?
    Oh, it's on the View layer, definately. The variables to be bound then come from either the Controller or the Model.


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
  •