Jump to content

MVC + whole presentation layer question


mr groovy

Recommended Posts

Hi,

 

I am new to phpfreaks.com, this is my first post.

 

I am writing a object oriënted framework with the Model View Controller concepts in mind, but have a question.

 

 

I know the basics of MVC: a frontcontroller recieves the URI and selects a controller to call the right action (function that is). Inside this action function models are being used for data manipulation and that is assigned to a view for display.

 

For the views I use a self made template system that can replace tags and supports nested blocks.

 

The basics of MVC are not really a problem, there are enough examples on the internet to be found (including the wiki of phpfreaks.com).

 

But my question is about the complete presentationlayer: the whole template where the view generated by the action function is being put into.

The template that wraps everything has logic of it's own like building a navigation with extra buttons depending on if the user is logged in or not.

 

Where to put this code? and how will it fit into the MVC pattern?

 

One way that pops in my mind is making a base class for all controllers.

Then subclass the base controller with common functions for layout logic.

Then subclass that controller to re-use the functions.

This way i can create a heirarchie for different layouts in the website.

 

I would really like to hear your thoughts about this design problem. Any help is welcome!

Link to comment
Share on other sites

Generally speaking, everything that's concerned with presentational logic belongs in the view part of MVC. This means that putting the controllers' methods would be a no-go. If you have a lot of shared functionality, which is commonly used in your views, then you obviously should not copy and paste it all over the place (which is exactly your problem). One answer would be creating view helpers. The implementation isn't set in stone (no patterns are), so you are more or less free to do it as you see fit. It's essentially a class/function/file that is given some input, and in return gives some output. I suppose you could call it a "sub-view" too, i.e. a view file invoked by another view file. You could take a look at the implementation in Zend Framework, which is my favorite framework.

 

I am new to phpfreaks.com, this is my first post.

 

Welcome :)

Link to comment
Share on other sites

Hi, thanks for the reply,

 

Really helpfull, I will look into view helpers.

First thing that showed up in google: http://blog.rvdavid.net/template-view-and-view-helper-design-patterns-in-php/.

 

The implementation isn't set in stone (no patterns are), so you are more or less free to do it as you see fit.

Ok, i thought views where not allowed to do anything, not even call functions. I thought they are more or less passive and only waiting to be assigned with values from smarter objects.

 

Things will fall into place now, thanks alot  :)

Link to comment
Share on other sites

Daniel0 is right on with good advice.

 

Just a couple random firings of my own:

-Why build a templating system with a new syntax instead of leveraging PHP itself as the templating system? There are plenty of tutorials that demonstrate this.

 

-In my framework which also loosely leverages MVC I introduce a concept of "Widgets." I'll basically create a view or a template but will sometimes drop some logic in it too, implement other third party scripts, etc w/o forcing it to fit into my frameworks native model. You can probably leverage view helpers or some other construct, just a matter of how much flexibility and/or standardization you're after.

Link to comment
Share on other sites

Hi,

 

Yes what you say does make sense to me.

I have used a template system before and i liked it, becouse it completely seperates PHP from html.

 

Using php inserted in a HTML file seems te give more freedom, becouse php can do anything i want when i want in the template. With a template i am stuck with the tags and blocks and need to write code to manage calls to template functions.

 

It was fun to write a templating engine, i have really learned alot about regular expressions through that (nested blocks where a real challenge). But if there is a better and simpler solution then i am allways open to it and will look into it.

Link to comment
Share on other sites

I'd just go with plain PHP in the template files. A project I'm working on now uses some stupid template system. I constantly have to refer to the documentation or look at existing source code to figure out how to do even the simplest things. It's really annoying.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.