Welcome Guest, Not a member yet? Register   Sign In
Some advice on structuring views with dynamic data?
#1

[eluser]mattpointblank[/eluser]
Hi everyone,

So my site has various content 'blocks', most of which are dynamic (eg 'most popular articles, 'recent comments', etc).

In my controllers, I have a structure similar to this:

Code:
<?php
$data['popular'] = $this->Review_model->getPopularArticles();
$data['comments'] = $this->Review_model->getRecentComments();
                        
$this->load->view('header/base_header', $data);
$this->load->view('header/base_body', $data);
$this->load->view('register');
$this->load->view('footer/base_footer', $data);
?>

I have several header files, basically the first one contains my <html> doctype and JS/CSS. After that comes the 'base_body', which includes the HTML/CSS that appears before my content. This also means that if any of my pages need extra JS/CSS, I can include them before including 'base_body', so I don't have to add them to the global header.

Anyway, I'm finding myself having to write more and more code just to get a 'normal' page, eg, the two $data calls are to get the dynamic content that's used in 'base_body' and other places. I don't want to break MVC style and move those calls to the views, but it's starting to reach the point where I need a dozen lines of code just to get all the data into my views. Is there a better way to organise this? Maybe put it all into a function or helper? Can anyone give me a poke in the right direction?

Matt
#2

[eluser]theprodigy[/eluser]
not sure if this helps any, but I have my stuff setup with a base template view.

I use MY_Controller to extend Controller. In the constructor, I create the data array that will be used to pass to the views
Code:
$this->data = array();

create the scripts and styles arrays
Code:
$this->data['scripts'] = array();
$this->data['styles'] = array()

and set a basic title
Code:
$this->data['title'] = "MySite.com";

I also have a show_page function that loads the vars and calls the template view.
By using
Code:
$this->load->vars($this->data)
the variables are usable no matter how many levels deep my views go, this way, my template view can load the page view and my variables will still be accessible.

In my controllers,
I add to the javascript and styles arrays any css or js that I need for that page
Code:
$this->data['scripts'][] = 'js/jQuery.js';
$this->data['scripts'][] = 'js/home.js';
$this->data['styles'][] = 'css/styles.css';
set the title if I don't want the default
Code:
$this->data['title'] = 'Home Page';

set the page view
Code:
$this->data['view'] = 'home';
and set any other needed variables, then call $this->show_page().

Inside my template view, I echo the title where needed, loop through the scripts and styles arrays outputting the needed tags, open the body tag, load the view
Code:
$this->load->view($view);
and then close off the body and html tags
#3

[eluser]jfox[/eluser]
I recently struggled with this as well trying to create one application used across a site that had different skins per major content category.

I ended up implementing something similar to what prodigy recommends, only I created a Base Controller (though I guess MY_Controller follows CI conventions better). I use that setup to define the base page title and view per his comments and override them as necessary in child controllers. So far that has worked well, been clean and easy to maintain. So I second that recommendation.

The script/CSS situation is another story.

I have been using smarty as my template engine and despite some careful pre-planning, I ended up with a mess of different functions in my controller to define what scripts went along with what controllers/functions. I use JQuery and ended up with numerous instances of actual JQuery code in both my helpers and controllers (a big no-no for me in an MVC setup) that was piped down and output by the view templates.

It got crazy enough that I yanked all that code out of the controllers/helpers and bit the bullet and started making actual view templates for each major page that required custom JQuery functionality. This keeps the view specific info in the view, but means I have more templates than I'd like. :down:

Still searhcing for the brass ring on this myself.
#4

[eluser]ToddyBoy[/eluser]
I'm having similar problems, as you can see in this thread - http://ellislab.com/forums/viewthread/121663/

I can't get my head around MY_Controller and it's purpose. Can anyone explain this? I've read a few threads and the user guide but haven't been able to understand it.
#5

[eluser]theprodigy[/eluser]
MY_Controller is a way of extending CodeIgniter's Controller class. It goes in your application/libraries folder.

You start the class by naming it MY_Controller and extending Controller
Code:
class MY_Controller extends Controller {
  function MY_Controller(){
    parent::Controller();
  }
...

then add any functions that you want all your controllers to have access to (like show_page() or get_menu() etc)

then close off your class.

Then all your controllers extend MY_Controller instead of Controller like they normally would.

Code:
class Home extends MY_Controller {
  function Home(){
    parent::MY_Controller();
  }
...

Now, all your controllers have access to the functions you created in MY_Controller through the $this operator
Code:
$this->show_page();
$this->get_menu();

Hope this helps
#6

[eluser]John_Betong[/eluser]
[quote author="mattpointblank" date="1249589194"]Hi everyone,

So my site has various content 'blocks', most of which are dynamic (eg 'most popular articles, 'recent comments', etc).
...
...
...
Anyway, I'm finding myself having to write more and more code just to get a 'normal' page, eg,
...
...
...
Matt[/quote]
 
Maybe this is not the best way but maybe you should consider it anyway. The idea is that the views, partial views, includes and also my CSS/JS files are all in the same directory and easily distinguishable.

1. All individual views are prefixed with view_ then the controller name.
2. All partial views and includes are prefixed with an underscore.
3. Style sheets are similar to views, ie style_ then controller name.


All view files are similar to this style.
Code:
<php echo $doctype ?&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;&lt;php echo $title /* set in the controller */ ?&gt;&lt;/head&gt;

  &lt;?php echo $header /* includes common CSS/JS files */ ?&gt;
  &lt;?php echo $oddball_header_001 ?&gt;
  &lt;?php echo $oddball_header_002 ?&gt;

  &lt;style type='text/css'&gt;
     /* debug information here */
     div {outline:solid 0px #f00}
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

  <div id='container'>

    <div id='left'>
      &lt;?php echo $left ?&gt;
      &lt;?php echo $left_001 ?&gt;
      &lt;?php echo $left_002 ?&gt;
    </div>

    <div id='middle'>
      &lt;?php echo $middle ?&gt;
      &lt;?php echo $middle_001 ?&gt;
      &lt;?php echo $middle_002 ?&gt;
    </div>

    <div id='right'>
      &lt;?php echo $right ?&gt;
      &lt;?php echo $right_001 ?&gt;
      &lt;?php echo $right_002 ?&gt;
    </div>

    <div id='footer'>
      &lt;?php echo $footer ?&gt;
    </div>

  </div?&lt;?php /*container */ ?&gt;

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

&nbsp;
&nbsp;
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB