Welcome Guest, Not a member yet? Register   Sign In
Loading views within a view file
#21

[eluser]jdavidson[/eluser]
[quote author="adamp1" date="1271160570"][quote author="jdavidson" date="1271127323"]Not sure if this is helpful but another possibility is to use a simple include within the view such as:

Code:
<?php include('system/application/views/header.php'); ?>
[..body code here...]
<?php include('system/application/views/footer.php'); ?>

As long as you pass all variables needed to the original view, there is no need to "load" additional views since as far as codeigniter is concerned you have only loaded a single view.[/quote]

I wouldn't use this method in an MVC framework since there is no need with $this->load->view. Another thing is the paths have to be absolute/relative which mean if you did change the folder structure you have to go update all include statements.[/quote]

You have a good point with the folder structure argument. That definitely makes it much more beneficial to load all views from the Controller. Thanks for the input.
#22

[eluser]Linderman[/eluser]
Ok , i am confused. Why? Because when i
Code:
<?php $this->load->view('global/header'); ?>
in my page view all $data passed to it is available for all widgets and parts i load in the page view ...

The Page view:

Code:
<?php $this->load->view('global/header'); ?>
<?php echo $template; ?>    
<?php $this->load->view('global/footer'); ?>

The header part (which is included in the page):

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/xhtml1-loose.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;title&gt;Codeigniter&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;!-- &lt;link rel="stylesheet" type="text/css" media="screen" href="/theme/css/application.css" /&gt; --&gt;
    &lt;style&gt;
        .header { border:solid 1px red; }
        .footer { border:solid 1px green; }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    
    &lt;!-- DEBUG: --&gt;
    
    &lt;!-- /END DEBUG; --&gt;
    <div class="header">
        <h3>Header</h3> &lt;?php echo $date . "test"; ?&gt;
    </div>

The controller:

Code:
class Infopage extends Frontend {

    function Infopage(){
        parent::Frontend();
    }
    
    function index(){
        $index['index_text'] = "Index lorem ipsum text";
        $data['template'] = $this->load->view('infopages/index', $index, true);
        $data['date'] = date("Y-m-d");
        $this->load->view('page', $data);
    }
}

as u can see i am passing the $data['date'] to the page -> then page includes header -> in the header i am calling $date ... and everything is perfect.

Why is that? Or maybe the author have different situation and me didnt understand? :red: If so ... sorry!
#23

[eluser]yingxuy[/eluser]
It really MVC structure. If we have the ability to do this using the methods defined in the Gadgets parser or, why we call another point of view.
#24

[eluser]porquero[/eluser]
I think that this is the best solution:

Controller
Code:
public function action()
{
   $data = array(
     'view' => 'view_name',
     'title' => 'Hello World'
   );

   $this->load->view('template_tpl', $data);

}

View
Code:
php + html

Template View
Code:
php + html
&lt;?php $this->load->view($view); ?&gt;
php + html
#25

[eluser]Unknown[/eluser]
I think I found the solution

Code:
&lt;?php $this->load->view('header');?&gt;
<p>Yak yak yak</p>
&lt;?php $this->load->view('footer');?&gt;

if you want to pass the variables to the next view, you can use the $_ci_data['_ci_vars']. as below:

Code:
&lt;?php $this->load->view('header', $_ci_data['_ci_vars']);?&gt;
<p>Yak yak yak</p>
&lt;?php $this->load->view('footer', $_ci_data['_ci_vars']);?&gt;

Hope it helps.
cya
#26

[eluser]InsiteFX[/eluser]
You dont even need to do that!
Code:
public function action()
{
   $data = array(
     'view'  => 'view_name',
     'title' => 'Hello World'
   );

   $this->load->vars($data);  // makes vars global to all views!
   $this->load->view('template_tpl');
}
#27

[eluser]dmayo2[/eluser]
InsiteFX and others are correct.

This thread came up in google on the subject. I've just tweaked my controller and view(s) to follow this scheme. At first, the benefits, not to mention keeping the level of abstraction that MVC provides, wasn't evident until the code was being written.

Sure the views have
Code:
$this->load->view($header);
calls, but notice that it's calling not a specific file, but a variable that references a specific file (header.php if you may). The abstraction and thus the benefit comes from setting that var in the controller. Therefore you don't have to to touch the views to modify what file is being called as the 'header' and the view does what it does -- output PHP vars and HTML leaving the logic to other components of the framework. Additionally, all vars within the $data object are global to all views (the 'pTypes' result set is available for output in the $content sub-view).

Code:
...
$data['pTypes'] = $types;              // assign mysql object results to var
$data['pageTitle'] = 'My Page Title';  // assign global var
$data['header'] = 'header.php';        // set filename for header sub-view
$data['footer'] = 'footer.php';        // set filename for footer sub-view
$data['content'] = 'sub/content.php';  // set filename for sub-view
...
$this->load->vars($data);
$this->load->view('dashboard'); // this loads the 'main' view
...
#28

[eluser]adityamenon[/eluser]
[quote author="dmayo2" date="1335969173"]InsiteFX and others are correct.

This thread came up in google on the subject. I've just tweaked my controller and view(s) to follow this scheme. At first, the benefits, not to mention keeping the level of abstraction that MVC provides, wasn't evident until the code was being written.

Sure the views have
Code:
$this->load->view($header);
calls, but notice that it's calling not a specific file, but a variable that references a specific file (header.php if you may). The abstraction and thus the benefit comes from setting that var in the controller. Therefore you don't have to to touch the views to modify what file is being called as the 'header' and the view does what it does -- output PHP vars and HTML leaving the logic to other components of the framework. Additionally, all vars within the $data object are global to all views (the 'pTypes' result set is available for output in the $content sub-view).

Code:
...
$data['pTypes'] = $types;              // assign mysql object results to var
$data['pageTitle'] = 'My Page Title';  // assign global var
$data['header'] = 'header.php';        // set filename for header sub-view
$data['footer'] = 'footer.php';        // set filename for footer sub-view
$data['content'] = 'sub/content.php';  // set filename for sub-view
...
$this->load->vars($data);
$this->load->view('dashboard'); // this loads the 'main' view
...
[/quote]

Indeed. Taking it a step further, I extended the core controller (http://ellislab.com/codeigniter/user-gui...asses.html) and set this $global_data + did load->vars($global_data) inside the __construct(). Now, this code does not need repeating in all the controllers too, just make sure you extend MY_Controller and not CI_Controller.
#29

[eluser]Lewis Cowles[/eluser]
I use a custom autoloaded model to allow views called from a Template model.

Code:
&lt;?php
class Template extends CI_Model
{

protected $header;
protected $footer;

// constructor required by all models
function __construct()
{
  parent::__construct();
  $this->header = 'themes/default/header';
  $this->footer = 'themes/default/footer';
}

function setHeader($view)
{
  $this->header = $view;
}

function setFooter($view)
{
  $this->footer = $view;
}

// display the template with header and footer
function view($view, $data = array())
{
  $data = $this->sanitize($data);
  $this->load->view($this->header, $data);
  $this->load->view($view, $data);
  $this->load->view($this->footer, $data);
}

// parse a template with header and footer
function parse($view, $data = array())
{
  $data = $this->sanitize($data);
  $this->load->library('parser');
  return $this->parser->parse($this->header, $data, TRUE);
  return $this->parser->parse($view, $data, TRUE);
  return $this->parser->parse($this->footer, $data, TRUE);
}

// sanitize data
function sanitize($data){
  // title
  if(!isset($data['title']))
  {
   $data['title'] = "";
  }
  // ... (there are tonnes here but they are ecosystem specific)
  return $data;
}
}
This allows me to display multiple views using the template class using basic simple, familiar syntax
Code:
$this->Template->setHeader('themes/default/header');
$this->Template->view('welcome_message');
You may notice that I edited the header and footer before using template to generate the content, this is due to the way the class works. Also this assumes the Template class is preloaded. It also works without the header and footer being set by using
Code:
$APPLICATION_PATH.'views/themes/default/'

hope this helps someone else!

P.S. you could also have data specific to the header and footer passed by adding a variable $data = null and two internal properties of the Template class, headerData & footerData
#30

[eluser]Unknown[/eluser]
thanks a lot. got the same problem with loading of files =)





Theme © iAndrew 2016 - Forum software by © MyBB