When is MVC not MVC?

Right gang,

I’m quite happy with what I’ve learned over the years, doing what most people do, learning the standard patterns, going through the big frameworks ultimately to end up going full circle and doing my own thing (based on all the valuable insight garnered here on Sitepoint of course…!)

For the new admin app we’re working on, I have a ‘Front Controller’ which does all the db and user authentication etc. I have several ‘Models’ each of which lets users perform a particular task.

I even have (in my opinion :wink: ) a nifty ‘View’ setup - each HTML view can contain placeholders like:


<!-- START PLACHOLDER - TABLE containing names and addresses. -->
<?= include 'sample_data/view6/object2.html'; ?>
<!-- END PLACEHOLDER -->

Those of you who spotted the ‘include’ were right - if I run the view through my app, I can read in the HTML with file_get_contents() then regex to find the include and replace it with the live data! The front end guys can call up the view on it’s own and it will load up some sample data so the page renders as it should without messing with my backend stuff. (In the future, the sample data will be linked to the database model to allow for changes.) Some views can be spat out as HTML, PDF or CSV… Happy days.

The problem is this: there are more and more ‘Views’ where it makes more sense for me to describe the HTML elements directly in my models. How can I have an MVC system if my V is in the M???

This isn’t special in the slightest.

The only thing you should be doing right now is going thru your code and removing every single stupid comment like the 2 you posted.

The comments in the HTML are trivial and are destined for the front end guys (if you think they are stupid, perhaps you’d like to mention why AlienDev???). I’ve only shown them here to highlight the fact the HTML you would normally only find in the View has found it’s way out of the View and into the Model/Controller…

Say, for example, I need a form control in my View to have a particular type of formatting and some Javascript validation based on a certain user type. The parameters for the validation exist at the model stage, so I can’t have them hard-coded into the View.

What I am asking is, how can I dynamically add raw HTML (or Javascript) to my View from my Model/Controller whilst sticking to the MVC pattern? Surely the only other way to deal with it is to incorporate some of the Controller logic in the View? Am I missing something or should I be looking at a different implementation?

I realise that for many people, the Javascript part can be a pain to classify as either Model, View or Controller - your thoughts are welcome!

Maybe Symfony’s Templating component can shed some light on how they achieve [URL=“http://components.symfony-project.org/templating/trunk/book/01-Templating-In-Five-Minutes”]what you ask.

:wink:

Nothing overly wrong about your approach…I would do something differently.

It sounds as though you have presentation logic mixing with your business logic which is a massive booboo, maybe look into using Smarty as a template engine to assist in enforcing clean separation of concerns in this regard.

Cheers,
Alex

Thanks for your pointers re Symfony and Smarty. I know that in Symfony I can use helpers to create HTML snippets etc, and I’ve used Smarty in the past, although my problem isn’t really in creating a template as my templates are already clean - but I want to mess them up! I went over the documentation last night and tried to find some examples on the web, but I’m still a bit confused…

Say my application returns an array of bookings. It is pretty trivial to output the data as a CSV file, XLS spreadsheet, PDF table, or even a CSS styled table using the right template from my View. Now say the app returns a form to edit an individual booking where the form controls depend on the user type. At the very least there would be some nested if/else or switch statements to display the right HTML/Javascript combination - the logic is in the View…!

I think what I’m getting at is, what about a sort of dictionary of HTML (or PDF/XLS/etc) code snippets in my database that I can piece together based on my Model that when assembled outputs the right HTML file (probably coupled with a decorator-type template)? That way I can serve a generic HTML form snippet, and all the different types of HTML descriptions and Javascript validation scripts I need depending on user type.

Does anyone have any examples of this type of situation? I’m still not sure if I’m really on the MVC train or perhaps I’m looking at some other pattern? How maintainable is a database of HTML snippets for the UI guys?!! Thanks for your help!

You might want to have a look at Zend_Form to see how they do this (although some people do think its inflexible for the real world). You create the form object based on adding zend_form_elements such as selects, buttons etc. The actual ‘base’ HTML e.g the <input type=foo/> etc is defined/rendered from a form view helper and you can add custom zend_form_elements for different tasks.

You can then keep the business logic of form creation/what user sees what elements etc inside the form class/model rather than having loads of conditional statements in the view.

From the frontend perspective, designers would just have <?=$this->form;?> or <?=$this->form->getElement(“myTextBox”);?> although the latter may still have issues of including conditional statements.

Have a read around Matthew Weier O’Phinney’s blog for a series of posts on using Zend_Form

I don’t really get the problem here. You would either use conditionals in the view or separate template files based on the user state. If the changes between each type are minimal than the former approach is the one I would go with. However, if the user type drastically changes the controls I would either create some type of transparent sub-controller/module or change out the template and use a state based switch for the controller.

Thanks for taking the time to reply!

@oddz - the user type could drastically change the control…! For example, I could have a datepicker; in one instance, the control is readonly or just a div containing the value, perhaps with a HIDDEN field. In another instance, it’s a SELECT drop-down. In another instance it could be a TEXT with Javascript to validate the value. I know I could have a bunch of conditionals in my View, but I’d effectively put the application logic into the View and the number of if/else statements in one template or the number of mostly similar templates could scare the UI guys! Also, if there is a change to the user types or what they can do, then all those templates need to change.

@romance - the Zend_Form approach is maybe similar to what I’m asking in that the HTML snippet for a form element lives elsewhere. However, as far as I can see, am I unable to let the UI guys edit the HTML other than overriding a class, which means that those elements are off limits to the UI guys. Say they wanted to redo the way a SELECT works, or write in a standard CSS class, or wrap the element with a div or something.

As an alternative, if I were to query a database and pull out the UI HTML elements that apply, rather than cycling through an if/else or switch, I could build the template bit by bit. The problem is, I’ve never seen this before!

Thanks guys, I know some of these questions might have obvious answers, but I’m at a loss!

Personally I don’t see the point in creating your own framework bar for the learning experience. I messed around for about 2 years trying to find a great framework and decided, “wait a second, I’ll make my own”. I then happened to talk to long-time programmer who now manages projects and told him I was writing my own framework. He said that if I was in his team, he would kill me. From that day on, I’ve used symfony. :smiley:

@aaronfallon - I think that’s the problem - I’m quite happy to plug away at our own framework as there are many things that don’t exist in other frameworks, ours is predominantly for custom administration systems and not blogs/photo galleries etc. At the same time though, I’m happy to look at other frameworks if they have a feature that we can steal. Unfortunately at the moment, I haven’t found any parallels!

@boatingcow I guess if you’re happy to put the time an effort in, it’s worthwhile doing. Take a second look at symfony though. I’ve been surprised at just how powerful it is.