Arun David
Arun David

Reputation: 2784

Codeigniter multiple views

I have loaded three views from my controller

$this->load->view('header_view');
$this->load->view('home_view');
$this->load->view('footer_view');

And consider in the header_view, I open a if statement and closing it in the footer

<?php
if(!$viewable){
echo "You cant view";
}else{
?>

And in footer_view i close the else

<?php
}
?>

But this code is giving me an error. Parse error: syntax error, unexpected '}' in header_view.php

Is it possible to open an if statement in one view and close it in another view???... or any alternate solution???...

Upvotes: 0

Views: 1506

Answers (5)

Jhourlad Estrella
Jhourlad Estrella

Reputation: 3670

Unlike HTML, PHP code blocks need to have an opening and closing tags. Why? Because PHP codes are parsed on the server before being sent to the requesting end (user) while HTML and js are the other way around.

This means that you cannot "cut" php code blocks (if, while, for, foreach, etc.) and put the other half on another file. It will not parse as a complete PHP code.

In order to make your code run try these codes:

<?php
// Make the necessary security validations here 
// then set the value of $viewable
// ...
///
// After that...

if(!$viewable){
   echo "You can't view this page.";
}else{
   $this->load->view('header_view');
   $this->load->view('home_view');
   $this->load->view('footer_view');
}
?>

...or if you want to keep your template header and footer:

<?php
// Make the necessary security validations here 
// then set the value of $viewable
// ...
///
// After that...

$this->load->view('header_view');

if(!$viewable){
   echo "You can't view this page.";
}else{
   $this->load->view('home_view');
}


$this->load->view('footer_view');

?>

I hope that's the one you're looking for. If not, tell me.

Upvotes: 3

mattumotu
mattumotu

Reputation: 1514

No you cannot start an if block in one view and close it in another.

I would have the decision making in the controller, something like:

$this->load->view('header_view');
if(!$viewable) {
    $this->load->view('denied_view');
}
else {
    $this->load->view('home_view');
}
$this->load->view('footer_view');

or even better, use a template view.

So your controller would be:

$data = array();
if(!viewable) {
    $data['main_content'] = 'denied_view';
}
else {
    $data['main_content'] = 'home_view';
}
$this->load->view('template_view', $data);

then template_view.php (in the view directory) would be:

$this->load->view('header_view');
$this->load->view($main_content);
$this->load->view('footer_view');

This would mean you don't have to load the header and footer views in every method on every controller so you are not repeating yourself and it looks neater too.

Upvotes: 1

tejash
tejash

Reputation: 167

Basically as per my experience its not good idea to travel code in between views. As view is separate entity and you never like to make dependent views. As its reduce re usability.

And its not possible to open an if bracket in one view and close in another.

I am 100% sure there must be some other way to achieve what you are trying to do

Upvotes: 2

thep
thep

Reputation: 176

What it means, better do that in view and let's view to manage header content, and footer. another to have better way is, you may find layout library.

Upvotes: 0

Sam Dufel
Sam Dufel

Reputation: 17608

Pass the name of the home view to your header controller, and load it there.

Controller:

$this->load->view('header_view', array('view_name' => 'home_view'));

header view:

if ($viewable) {
  $this->load->view($view_name);
}

Upvotes: 1

Related Questions