-1

I'm looking for a solution to loading the header/footer/navigation only on the initial page view.

The reasoning behind this is that my navigation panel uses Ajax to render the content of the destination link in the main content div instead of actually loading the page itself.

However, I need to be able to say something along the lines of if its the initial view, load the header, navigation, then the actual page, then the footer. But if its not the initial view and the page is loaded in the content div area so for example a navigation link or a form submit, it doesn't load the header, navigation and footer but only the actual page.

So for example I have these 4 views:

header_view navigation_view page_a_view footer_view

And this controller:

controller_a

The navigation has a link which is 'Page A' and points to http://myurl.com/controller_a. If that link is clicked I only wanted controller_a to load the page_a_view view, because the navigation link will load the data of that page into the div.

However if I directly go to http://myurl.com/controller_a, it needs to render the the header/navigation/footer also.

The solution I thought I was on the right lines with was something like a base controller with a function load_page() which would be something like:

function load_page($page, $data) {

    if($this->uri->segment($this->uri->total_segments()) == "initial_page_load") {
        $this->load->view('header_view');
        $this->load->view('navigation_view');
        $this->load->view($page, $data);
        $this->load->view('footer_view');
    } else {
        $this->load->view($page, $data);
    }

}

So if I was to go to http://myurl.com/controller_a/initial_page_load it would load the header/navigation/footer but if I went to http://myurl.com/controller_a it wouldn't.

However this doesn't cover all possibilities.

Another way I thought of is checking if there is a referring URL, if there is, don't load the header/navigation/footer. But again, this doesn't cover things such as what if the link was referred by an external website?

Does anyone have any suggestions to how I can achieve what I need?

4

3 回答 3

1

让它成为所有视图的视图页面。对于您的其他页面,您将通过 ajax 动态更改 div 上的内容“page_content”。

**application/views/your_template_view.php**
<?php
    $this->load->view('header_view');
    $this->load->view('navigation_view');
?>
<a href="javascript:void(0)" id='home' class='my_links'>Home</a>
<a href="javascript:void(0)" id='about_us' class='my_links'>About us</a>

<!--
    Make sure the id of the page is same as 
    the file name of view page for respective pages
 -->

<div id='page_content'><?php echo $page_content; ?></div>

<?php
    $this->load->view('footer_view');
?>
<script lang='javascript'>
    $('.my_links').click(function(){
        $.post(
            '<?php echo site_url('test_controller/get_page_content') ?>',
            {'page_content':$(this).attr('id')},
                function(response){
                    if(response != '')
                        $('#page_content').text(response);
                },'json'
        );
    });
</script>

让它成为你的控制器:

**application/controller/test_controller.php**
function your_page($param1 = '', $param2 ='') {
    /*this one is for your initial page*/
    if($param1)
        // send mail
    if($param2)
        // send mail 
    $data['page_content'] = $this->load->view('initial_page_load');
    $this->load->view('your_template_view', $data); 
}
function get_page_content()
{
    /*this one is for other pages, this provides page content 
      for the request made from ajax*/
    $page_title = $this->input->post('page_title');
    $page_content = $this->load->view($page_title);
    echo json_encode($page_content);
}
于 2013-04-25T11:28:18.137 回答
0

制作一个会话变量来表示是否加载了页眉/页脚,如果是,则跳过它们

if (!$this->session->userdata('nav_loaded')) {
    $this->load->view('header_view');
}

ETC...

这将适用于您的所有页面,无论它们是从哪里调用的,只要所有初始化后调用都由 AJAX 完成。

编辑

如果你想在那之后混合 ajax 和非 ajax 调用,当然会话变量将不起作用。也许您可以在您的 url 中放置一个 ajax 参数并据此调整控制器。

public function index($ajax = false) {
...
    if (!ajax) {
        $this->load->view('header_view');
    }
    etc...
}

因此,所有的 AJAX 调用都会将 url 中的附加参数设置为 true(几乎除了零之外的任何参数:index/1index/ajax等)。

于 2013-04-25T11:54:51.190 回答
0

尽管我仍在对此进行测试,但到目前为止它似乎完全符合我的预期!它是那些简单但完成工作的那些之一,所以希望没有问题弹出......

在 MY_Controller.php

function load_page($page, $data) {
    if(!$this->input->is_ajax_request()) {
        $this->load->view('header_view');
        $this->load->view('navigation_view');
    }
    $this->load->view($page, $data);
    if(!$this->input->is_ajax_request()) {
        $this->load->view('footer_view');
    }
}

在查看 Input 类文档时设法偶然发现了该功能

于 2013-04-25T13:16:08.387 回答