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 ) 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…
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.
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…
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!
@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.
@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.