Welcome Guest, Not a member yet? Register   Sign In
help with view within a view
#4

[eluser]bigtony[/eluser]
I've found this simple approach works for me:

Controller: somepage.php
Code:
<?php

class Somepage extends Controller {

  function Somepage() {
    parent::Controller();    
  }

  function index() {
    $view_data['title'] = 'Title of page';
    $view_data['h1'] = 'H1 page header text';
    
    $view_data['somedata'] = 'Some data for the page;
    
    $view_data['view_template'] = 'somepage_view';
    $this->load->view('layout_view', $view_data);
  }

}

?>

View: layout_view.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>

<html>
  <head>
    <title><?php echo $title; ?></title>
  </head>
  <body>
    <?php if (isset($h1)): ?>
      <h1>&lt;?php echo $h1; ?&gt;</h1>
    &lt;?php endif; ?&gt;
    
    // following line loads the content specific to this page.
    &lt;?php echo $this->load->view("{$view_template}"); ?&gt;

    // other stuff can go here that appears on all pages, eg. page links.
  &lt;/body&gt;
&lt;/html&gt;

View: somepage_view.php
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?&gt;

<p>&lt;?php echo $somedata; ?&gt;</p>

As you can see, each controller sets a value in the 'view_data' array with the name of the view specific to that page. Controller then calls 'layout_view' to take care of the headers, footer, etc. common to each page, and this view in turn calls the specific page view as per the 'view_data' variable value. All data in view_data is cascaded to each view automatically.

Obviously above example is bare-bones just to show the principle. It's not a perfect mvc solution as you end up putting display text in the controller (eg. title), but it's simple and it works.


Messages In This Thread
help with view within a view - by El Forum - 03-15-2009, 06:47 PM
help with view within a view - by El Forum - 03-15-2009, 07:35 PM
help with view within a view - by El Forum - 03-20-2009, 02:01 PM
help with view within a view - by El Forum - 03-21-2009, 03:46 AM
help with view within a view - by El Forum - 03-21-2009, 05:08 AM
help with view within a view - by El Forum - 03-21-2009, 05:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB