Welcome Guest, Not a member yet? Register   Sign In
Wrapping content in layout after execution (need some opinions)
#1

[eluser]mycroes[/eluser]
I'm managing a growing application, that will probably keep on growing for another few years. In about every (controller-) function that actually outputs something I'm loading a header and a footer view. In my header I display the title (as set by $this->load->vars('title')) but that's about as dynamic as it gets. I'm also going to add breadcrumbs, but those are just a combination of controller global data + title. Occassionally I output json (javascript object notation) and maybe I'll have other content types to offer in the future that should not get wrapped.

What I want:
1 controller specific setting of title
2 ability to turn layout wrapping off

The best thing would be if I could trigger the wrapper to off-state by throwing a header like 'Content-type: text/javascript' (or whatever it should be for my content), because that would save me some duplicate lines of code.

Options I figured out this far are:
a Setting a hook to wrap when everything has happened inside the controller
b Extending a default controller (will probably do this for other uses too) with layout functionality
c Load a library, call a function in it

Things wrong with options I figured out:
a Setting a hook to wrap
Hooks are dirty. I have not read much about real life usage of hooks, but the documentation makes me want to avoid hooks. Also, I can't set hooks from controllers, so point 1 only half works (I can still set data from within the controller)

b Extending a default controller
Currently the most appealing option for me... I can create a __destruct function (class destructor, using PHP5, don't care for portability) where I wrap all current output in the header and footer, seems like it should work. Turning it on is as simple as extending a controller, turning it off controller-wide is as simple as setting a variable or not extending the controller, and of course combinations allow for easy turning on for just one function, although I could also wrap the layout code in a library, load the library from the default controller and if I don't want to extend the default controller I can load the library by hand. It would be possible to also use a constructor and there start an output buffer, close the output buffer in the desctructor and then just echo header . buffer . footer, that currently seems like total win to me. Still, I don't think there's a way to catch the header in here...

c Create a layout library
Possible, but would mostly consist of getting content, returning header . content . footer... Also would have to call this at the end of all functions that want to include header and footer, that's exactly what I'm running from (although it would take less lines).

I hope someone can shine some light on how he's done this. I've seen some posts about using templates, but most of the implementations are dirty, a.o. involving reroutes and then reimplementing part of codeigniter functionality to load the real controller. I think option b is best, but maybe someone has some good points why I shouldn't create a base controller and extend it in the way I described, other than non-portability when using constructors and destructors.
Regards,

Michael


Messages In This Thread
Wrapping content in layout after execution (need some opinions) - by El Forum - 10-29-2008, 05:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB