开发者

Php - What is the proper way to accomplish header/footer extraction from pages?

开发者 https://www.devze.com 2023-03-24 05:54 出处:网络
Being new to php I really want to know how this is accomplished. You have a header and footer which are always present on the page and the content is the only thing different. So if you want to change

Being new to php I really want to know how this is accomplished. You have a header and footer which are always present on the page and the content is the only thing different. So if you want to change the header or footer you don't have to change it on each page.

1. include(...)

After reading some forums most people don't recommend this and solved this by using a template engine.

2. Template Engines

I thought this would be the answer but its just nice looking, masked, php code. I looked how a template engine could solve the initial problem; template inheritance. So after using Smarty template engine with inheritance I thought it was wonderful, worked perfect and was very easy 开发者_StackOverflow社区to extract code into parent templates.

But another problem arose. After coding my own small frameworks using smarty I wanted to try out CodeIgniter and make an application. The template engine they use is pretty basic and they don't recommend it. After finding out I cannot use template inheritance, for the header and footer problem, I looked for a solution, I found a library I can use to achieve inheritance, but it is built upon the already existing CI template engine, which doesn't offer the functionality I need.

Now I'm stuck and don't know what to do. What do you guys use to separate the header and footer from pages in php? I probably wrote too much but I want to know the proper way to do this. Thanks in advance.


If you're using codeigniter, just use different views for that..

class Content extends CI_Controller {

   function index()
   {
     $data['title'] = 'This is page one';
     $this->load->view('header', $data);
     $this->load->view('page_one');
     $this->load->view('footer');
   }

   function page_two()
   {
    $data['title'] = 'This is page two';
    $this->load->view('header', $data);
    $this->load->view('page_two');
    $this->load->view('footer');
  }
    //and so on..
}

In header.php you just echo $title; and you'll see it's different if you're on page one or page two. Anyway, you should maybe use a controller for each page, instead of a method as I did in this example, I was just showing a possibility without writing too much code.

Also, you can load views inside views, and the latter inherit data passed to the main view. Using the example above:

$data['page']['title'] = 'Page one';
$data['foot']['whatever'] = 'Whatever you want';
$this->load->view('page_one', $data);

view page_one.php

  <?php $this->load->view('header', $page); ?>

   <!-- html and php stuff for this page only -->

  <?php $this->load->view('footer', $foot); ?>

In header.php :

<title><?php echo $title; ?></title>

In footer.php:

<p><?php echo $whatever; ?></p>

Header.php and Footer.php are separate 'blocks', just 2 files you can load wherever you want. It's much the same as include(), if you look well at it. That's not really a templating engine, I know, but sometimes templating engine is "too much", and useful if other non-programmer are messing with layouts. If what you wanted to achieve was just a separation of page parts, splitting each page into blocks you can assemble at need is going to be easier then implementing Smarty or other template engines.


Maybe you can use this template library for Codeigniter. It is slightly better than the default templating engine and is really easy to use. You can set default "regions" for each template.


Code Igniter - how would you structure this site?

<?php

 class MY_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    public function loadView($view) {
        $this->load->view('header');
        $this->load->view($view);
        $this->load->view('footer');
    }
}

and if you need to load data in the header or footer:

$data["title"] = 'About us';
$this->load->view('header', $data);
0

精彩评论

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