Welcome Guest, Not a member yet? Register   Sign In
Create a WebSite - Template/Multiple Views
#1

[eluser]fabioneves[/eluser]
Hello there,

I've started using CodeIgniter today and I am having a big trouble create a template for my website.

Let's see, i've read all the user guide and it's fantastic, but it fails to demonstrate how to create a simple template..

I saw in the user guide that we can load multiple views and I successfully loaded them, but there's a problem, how can a view load a dynamic content that is supposed to be loaded from another controller?

Let's see, I have a controller named 'Page', where I load all the views that build up the template, but let's imagine I wanted to load in the sidebar the last 10 comments or something, how would I do that?
Code:
$this->load->view('default/header');
$this->load->view('default/menu');
$this->load->view('default/content', $data);
$this->load->view('default/right');
$this->load->view('default/footer');

$data would be the first content I want to load, it'll be generated in the Page controller, but the others belong to other controllers..

Loading like that, I can build the layout pretty well, my doubt is just how can each view load their respective content?

Dunno if I made me clear, but I'm a little confused right now..

Thanks in advance!
#2

[eluser]TheFuzzy0ne[/eluser]
$this->load->vars(). Just be careful of name collisions, which will cause you to overwrite previously set data.

I like to grab the content view, and insert it into the main template.
#3

[eluser]fabioneves[/eluser]
Thanks for your reply!

That would be in the controller? Anyway, when I call the first controller 'Page' with that load->view stuff, how would the controller of the latest blog posts be called for example?

My doubt is, you call a controller and load the views, that's fine, I got there. But I do I load the other stuff like a login form on the top, the latest blog posts, etc.. ?
#4

[eluser]jedd[/eluser]
Quote:I saw in the user guide that we can load multiple views and I successfully loaded them, but there's a problem, how can a view load a dynamic content that is supposed to be loaded from another controller?

The trick is to not get yourself in this situation! Wink

If you have code that is required in multiple controllers, then it arguably shouldn't be in a / many controllers - it should be elsewhere, say a library. DRY and all that.

Setting up an architecture where one controller needs to call another will twist you up in knots, and if you only started using CI just recently, you don't want to twist yourself intentionally just yet - there'll be plenty of time for that to happen later.

I see that your code snippets suggested that there was no $data being input to the majority of those. You may wish to explore the option of generating those from within MY_Controller - this is done by extending the CI controller, basically intercepting the normal Controller method that you extend when making your normal controllers.
#5

[eluser]fabioneves[/eluser]
thanks for trying to help me, but i'm getting even more confused..
what looked simple is beginning to look very complicated..

let's say it like this, how do you create a simple website with header / menu (horizontal) / content / rightbar with some custom 'modules' / footer ?
* the menu is dinamically loaded from the database, how would you proceed?
* the right modules will not always be the same

can you please describe me how would you proceed? if you know some good tutorial that demo's this I would appreciate, thanks a lot and sorry
#6

[eluser]jedd[/eluser]
Quote:thanks for trying to help me, but i'm getting even more confused..

This is perfectly normal. Things get worse before they get better. Try to relax. Pretend you're on a beach.

With a very nice laptop.

Okay .. you're asking for something that is quite normal, and lots of people have trodden this path before, so I'm sure we'll be fine.

Quote:let's say it like this, how do you create a simple website with header / menu (horizontal) / content / rightbar with some custom 'modules' / footer ?

Okay - what I've done is pilfered someone else's code. I took the idea from [url="http://ellislab.com/forums/viewthread/106945/#538402"]this post[/url] and modified it slightly.

All my pages are rendered by this view file - default.php :

Code:
<html>
<head>
<?php
    // Grab the theme to use.
    $theme = $this->session->userdata('theme');

    // Use the right CSS
    echo link_tag('assets/stylesheets/'. $theme .'.css')
?>

<?php // jquery loader goes in here , but is stripped by CI Forum for security reasons ?>

<title>
    <?= $title ?>
</title>
</head>
<body>

<?php
?>

<div id="div_everything_wrapper">
    
    <div id="div_topbar_wrapper">
        &lt;?php echo $top_bar_view; ?&gt;
    </div>

    <div id="div_torso_wrapper">

        <div id="div_navigation_menu">
            &lt;?php echo $main_menu_view; ?&gt;
        </div>

        <div id="div_main_content">
            &lt;?php  echo $main_text_view;  ?&gt;
        </div>

        <div class="break">
        </div>

    </div>

    <div id="div_footer_wrapper">
        &lt;!-- Don't want to pre-render this or it throws our profiling out --&gt;
        Rendered in {elapsed_time}s, and {memory_usage}.
    </div>
</div>


&lt;/body&gt;
&lt;/html&gt;

The left/right/top/bottom stuff you mention is obviously sorted by CSS, but I gather you're hip with that? Happy to upload my rather exuberant CSS file if you're interested .. and masochistic. Obviously, in the above, my navigation menu is on the left, main content is on the right, and I've got top and bottom bars of varying complexity.

Now, equally obviously, all those data I refer to in there need to get generated somewhere.

They all come in as part of the $data array from whichever controller calls this default view. But note that each of my controllers generates (as a rule) ONLY one variable - the $main_text_view component, which is obviously the meat in the web sandwich. All the other bits of data there are generated in MY_Controller.

I've actually just written a wiki page - [url="http://codeigniter.com/wiki/MY_Controller_-_how_to_extend_the_CI_Controller/"]MY_Controller - how to extend the CI Controller[/url] which you might want to have a read of now. Let me know if it needs any modifications.

In my MY_Controller file I make topbar_view, title, and the navigation_menu and insert them all into $this->data at that point (so they appear in $this->data in all your controllers too - you're hip with this side of things, yes?

Quote:* the menu is dinamically loaded from the database, how would you proceed?

Okay, slightly more complex, but not hugely so. You should be able to talk to your Model via MY_Controller - though I haven't actually done this. I'm not sure when the database class gets loaded - but I'd imagine far enough back up that it is usable (I'm an optimist at heart).

Certainly if you work through this stuff above, and get stuck at the dynamic / database generation of your menu, there'll be plenty of interest in helping you over that hurdle.
#7

[eluser]fabioneves[/eluser]
ok, got it all ! I'll try to do it tomorrow since I just came back from football and i'm really tired.
I really appreciate your help, you cleared me a lot of things, I think this will do!!

Thanks again !! Smile
#8

[eluser]bretticus[/eluser]
I've been using CI for about 6 months or so now. I've only done 2 projects with it thus far, but as for passing a dynamic menu, it's very easy to pass the data to the current view (the one view you call from your controller at a given instance) and then call a menu view from within the same view. For example, you pass a $data array to your view when you call...

Code:
&lt;?php
$this->load->view('default/content', $data);
?&gt;

...within your content view you can use the same code to call other views...

Code:
&lt;html&gt;
&lt;body&gt;
&lt;?php $this->load->view('default/menu'); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

The information in $data is also available to the views called within other views. In other words, views are just PHP pages and have access to the variables you pass them ($data.) So, where you might use a PHP or server-side include, just call another view instead. If you have the LAYOUT code necessary to show/build a menu in your menu view, again, the $data variable is in scope (no need to pass it as the 2nd parameter.) I think that when you start having your controller "paint" out the content for you (loading multiple views at once in controller) you lose the flexibility that the MVC pattern was designed for. It's much easier to edit your layout when it's clear how your layout is being put together in context. Just my opinion. I'm interested in how others have implemented dynamic redundant content into their views.




Theme © iAndrew 2016 - Forum software by © MyBB