开发者

Codeigniter multiple views

I have loaded three views from my controller

$this->load->view('header_view');
$this->loa开发者_如何学God->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???...


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.


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


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);
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜