Loading views within a view file |
[eluser]wrs[/eluser]
Hi, I'm using multiple view files for various elements of the rendered page, such as the header, footer, and other widgets. CodeIgniter docs suggest I should be loading these views entirely from the controller like so: Code: <?php Code: <?php $this->load->view('header');?> This way I only have to load the 'user/register' view from the controller, but is it good practice? Are all the benifits of using views ( as opposed to just echoing data ) still intact?
[eluser]sophistry[/eluser]
yes, but you have to use Code: $this->load->vars(array('var_name_you_want'=>'value')); somewhere (controller, generally) to get any variables defined ... or you could define them in the view (ick) and pass them as the second parameter.
[eluser]Colin Williams[/eluser]
The simplest method is to have a $data property that is available in all methods of the given controller. Then you use this property to assign global and controller method specific views variables: Code: class Blog extends Controller { If you prefer abstraction on an application-wide scope, you can take a look at my Template library, linked in my sig below:
[eluser]wrs[/eluser]
Two nice variations, thanks! My end goal is to have CodeIgniter serve XML documents and use client side XSLT to render useful pages. I'm hoping I can still achieve this using views, but I know hooking or rewriting the output class is another option. Theres many great features in CI, just takes a bit of getting used to
[eluser]sophistry[/eluser]
@Colin Williams... I'd submit that using $this->load->vars() CI loader method is "simpler" than adding a class variable to your controller. And it's shorter. Unless by "simpler" you mean "I prefer it" ;-) This shows a typical use (used in the constructor) of the vars() method of the Loader class. And, incidentally, it's also the way I prefer to do this sort of thing. Thanks Colin for a nice Template to work from! :-) Code: class Blog extends Controller {
[eluser]prezet[/eluser]
How would you handle passing over a page title in this case? or if there a better way of handling page titles?
[eluser]TB[/eluser]
[quote author="prezet" date="1219188638"]How would you handle passing over a page title in this case? or if there a better way of handling page titles?[/quote] Exactly what I was wondering about.. Can someone please answer this?
[eluser]cozzmyn[/eluser]
I'm doing this way: I'm breaking the whole template into partial views (header, content, footer ...) and I'm using another view called container which binds them together. The content view is passed as a string to the container view by the controller an the rest are just loaded directly by the container. The code looks something like this: The views: file: container_view.php Code: <?php file: header_view.php Code: <html> file: footer_view.php Code: </body> file: some_content_view.php Code: <?= $some_content; ?> The controller: file: Test.php Code: <?php Cheers!
[eluser]kurucu[/eluser]
[quote author="sophistry" date="1218865464"]@Colin Williams... I'd submit that using $this->load->vars() CI loader method is "simpler" than adding a class variable to your controller. And it's shorter. Unless by "simpler" you mean "I prefer it" ;-) This shows a typical use (used in the constructor) of the vars() method of the Loader class. And, incidentally, it's also the way I prefer to do this sort of thing. Thanks Colin for a nice Template to work from! :-) Code: class Blog extends Controller { Just to confirm.... does this mean that the variables available to views are as follows? blog/front: header, footer, blogs template: header, footer, blogs, content So in other words, $this->load->vars is appended to by any data you submit later?
[eluser]sophistry[/eluser]
yes, the vars() method of the loader class allow a kind of "global" variable that is available to any view. The Loader Class Documentation |
Welcome Guest, Not a member yet? Register Sign In |