views:

67

answers:

1

I came to a point where I realized that I need an intelligent solution for this problem.

In my framework, there are:

  • Views (actually just HTML files with little PHP to output data)
  • View Controllers (create views, do some logic)

So for every page like http://stackoverflow.com/questions/ask for example, I have an ViewController which is called by the framework. There's an RootViewController which creates an layout view. The layout view specifies headers and footers, and the main structure of the site. It has a contentView placeholder, for instance.

With this design I specify the HEAD part only once, and so the DOCTYPE, TITLE and all the other stuff is sticked to that layout view template.

Every View Controller which loads that RootViewController can set the title, append an CSS inclusion or Javascript inclusion (which is one of those fancy link rel tags in the HEAD), just by calling some methods on the RootViewController.

So far, that's actually pretty flexible, because if you don't specify anything, you get useful defaults and an reasonable HEAD with everything needed.

But I see these problems:

  • Can't I make life for the framework user more simple? The layout template must contain all this "garbage" that kills productivity. Why should a framework user even have to think about setting up a correct HEAD? It's always the exact same problem, needs the exact same syntax, the exact same inclusions of CSS and Javascript of the framework, etc. ...you get the point.

  • It would have to contain the exact corresponding placeholder variables to match the methods of the RootViewController.

So to solve this, I thought I could provide an fancy variable which can be inserted into any view template to output the HEAD block.

I just want to see what you think about this, and if my solution is just fine, or if I'm about to make a big design error here. And if so, what would be the alternatives?

+2  A: 

You could have your "user" specify only the body template, and automatically output your head, the "user's" body, and your footer. Then the template designer doesn't have to remember to include some arbitrary variable, and should you ever decide to change the way your header/footer are included, you won't have to update every single template.

You could also allow the template designer override this behavior by calling some function.

This is probably ideal: 90% of the time, your framework would do the right thing without any sort of developer effort. Only in the 10% of cases where the user wants to do something else, must they go out of their way to make it happen via a function call.

Frank Farmer