[eluser]TWP Marketing[/eluser]
[quote author="TWP Marketing" date="1333566485"][quote author="benners" date="1333561448"]I have a template.php View file and if $side_content is not set I want to load default content but this content requires a call to a modal to get the data to build it. How can a I call or load this from within the template.php View?
Code:
<?php if (isset($side_content)) {
$this->load->view($side_content);
} else {
// Get side panel data
$side_quiz_gallery = $this->quiz_model->get_live_quizes();
$data['side_quiz_gallery'] = $side_quiz_gallery;
$data['side_content'] = 'side_quiz_gallery';
} ?>
[/quote]
Instead, pass the data to the view using the $data array. I assume the view file is named 'template'.
IN THE CONTROLLER:
Code:
<?php
$data = array(); // declare array (may not be needed if already declared
if ( isset( $side_content ) ) {
// use previously defined content
$data['side_content'] = $side_content; // Is this a string, a column title or is it html code?
$data['side_quiz_gallery'] = ""; // empty string since there is no quiz by default
} else {
// Get default side panel data
$data['side_quiz_gallery'] = $this->quiz_model->get_live_quizes(); // Does the model output HTML code, containing the quiz itself?
$data['side_content'] = 'side_quiz_gallery'; // simple string. Is this the column title?
}
$this->load_view('template', $data); // load the view with data array
?>
[/quote]
@benners,
The code above should run in the controller, NOT in the view.
It creates the values to be displayed and then passes them to the view.
In the view, just echo the two vars;
$side_content and
$side_quiz_gallery, at the appropriate locations
IN THE VIEW:
Code:
...
<?php echo $side_content;?>
...
<?php echo $side_quiz_gallery;?>
...
This is good MVC practice. Massage your data in the controller. Only content is sent to the view.
If you see a need to massage the same data in each of your controllers, move that code to a model method and avoid repeating it.