CodeIgniter Forums
Basic Page Function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: Basic Page Function (/thread-16.html)



Basic Page Function - Tux - 10-22-2014

Load "header", "footer" and "content" all at same time by using simple "page()" function.

Site.php Model:
Code:
<?php defined('BASEPATH') OR exit('Access Denied');

class Site extends CI_Model {

    function page($page, $data = NULL) {
        $data['base'] = base_url();
        $data['page'] = current_url();
        $this->load->view('header', $data);
        $this->load->view($page, $data);
        $this->load->view('footer', $data);
    } #page

} #class

#EOF: ./application/models/Site.php

Make sure you autoload this "Site.php" model in your config. You need to edit "application/config/autoload.php" file and add "site" to model autoload array.

Usage in Controller:
Code:
<?php defined('BASEPATH') OR exit('Access Denied');

class Welcome extends CI_Controller {

    public function index() {
        $data['title'] = 'Welcome Page';
        $this->site->page('welcome', $data);
    } #index

} #class

#EOF: ./application/controllers/Welcome.php

header.php view:
Code:
<!DOCTYPE html>
<html lang="en">
    <head>
        <title><?=$title?></title>
        <meta charset="utf-8">
        <meta name="robots" content="all">
        <link rel="stylesheet" href="<?=$base?>/style.css">
    </head>
<body>

welcome.php view:
Code:
<div id="content">

  <h1> Example Welcome Page! </h1>

</div> <!--#content-->

footer.php view:
Code:
<div id="footer">
All rights reserved.
</div> <!--#footer-->
</body>
</html>



RE: Basic Page Function - Rufnex - 10-22-2014

You can also load it also directly inside the view:

some-page-template.php
PHP Code:
<?php $this->load->view('header'); ?>
<?php $this
->load->view('navigation'$data); ?>
... content ...
<?php $this->load->view('footer'); ?>

And here the call in the controller:

PHP Code:
$this->load->view('some-page-template'$this->data); 

Nothing new i think ;o)


RE: Basic Page Function - John_Betong - 10-22-2014

Returning views as data

From the manual:

There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:

$string = $this->load->view('myfile', $data, true);
echo $string;


RE: Basic Page Function - John_Betong - 10-22-2014

Duplicated post


RE: Basic Page Function - Rufnex - 10-22-2014

Yeah usefull for e.g. as email template Wink


RE: Basic Page Function - navotjer - 10-24-2014

I usually use MY_Controller and load the view like so (example only)(if anybody has some thoughts about my way, please, make me smarterSmile ):

Code:
class MY_Controller extends CI_Controller
{
    /*
     * Everything else you need
     */

    protected function render($views, $data = FALSE, $css = FALSE, $js = FALSE)
    {
     //get BASE css files, append new if needed
     $head['css'] = $this -> addCss($css);
     //metadata
     $head['metaData'] = $this -> metaData;
     //get BASE scripts, append new if needed
     $addJs['js'] = $this -> addJs($js);
     $this -> load -> view('page/head_view', $head);
     $this -> load -> view('page/header_view');
     $this -> load -> view('page/sidebar_view');
     if ($views === FALSE)
     {
    $this -> load -> view('page/content_view', $data);
     }
     else
     {
        if (is_array($views))
        {
            foreach ($views as $view)
        {
            $this -> load -> view($view, $data);
        }
        }
       else
       {
           $this -> load -> view($views, $data);
        }
     }
    $this -> load -> view('page/content_end_view');
    $this -> load -> view('page/footer_view', $addJs);
    }
}

Then in Some_controller:

Code:
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Index extends MY_Controller
{

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

    public function index()
    {
                $data = $this -> getDataFromSomewhere();
        $this -> render('', $data);
                //Or if I want a special "content" view
                $this -> render('name_of_view',$data);
    }

}

Thank you for the #EOF tip.
*Edit -> BB code tags...


RE: Basic Page Function - jlp - 10-25-2014

(10-22-2014, 12:48 AM)Tux Wrote: Load "header", "footer" and "content" all at same time by using simple "page()" function.

Site.php Model:
Code:
<?php defined('BASEPATH') OR exit('Access Denied');

class Site extends CI_Model {

    function page($page, $data = NULL) {
        $data['base'] = base_url();
        $data['page'] = current_url();
        $this->load->view('header', $data);
        $this->load->view($page, $data);
        $this->load->view('footer', $data);
    } #page

} #class

#EOF: ./application/models/Site.php

Speaking as an educator, I see MVC as a design pattern to separate state from presentation from control, and I encourage my students not to mix things up.

For instance, if my students submitted work where one of their models loaded any view stuff, they would be penalized marks. Similarly, if they submitted work where a view component accessed a database, they would be penalized marks too.

To me, the proper place for the "page" method you show is in the controller. If this were something used frequently, I would go so far as to put it in a base controller (core/MY_Controller).

Just trying to encourage good practices!


RE: Basic Page Function - kilishan - 10-25-2014

I will create a simple `render` method in MY_Controller that allows for a very simple type of template system. It will auto-determine the the view to be used based on controller/method naming conventions. A simplified version looks a little like this:

Code:
protected function render($data=array(), $layout='')
{
  $dir = $this->router->fetch_directory();
  if (! empty($dir)) $dir .= '/';

  $view = $dir . $this->router->fetch_class() .'/'. $this->router->fetch_method();

  $data['view_content'] = $this->load->view($view, $data, TRUE);

  $layout = ! empty($layout) ? $layout : 'index';

  $this->load->view('theme/'. $layout, $data);
}

My current one is actually a bit more complex than that, and automatically formats and inserts status messages, gets stylesheet and script links to the view, etc.

Then, I have different layout files in the views/theme folder that contain the overall layout, or template, to use. The content of the primary view for that method is made available to the layout in the $view_content variable. A simple layout might be like this:

Code:
$this->load->view('theme/parts/header');
$this->load->view('theme/parts/navbar');

<div class="container">
    <?= $view_content; ?>
</div>

$this->load->view('theme/parts/footer');

You can create as many layouts as you need - for example, two-column layouts, or blog specific layouts, etc. But then using them in your controller is a single method call:

Code:
public function index()
{
    $this->render();
}

// or

public function index()
{
  $data = array();
  // Do stuff here that puts data in the $data array
  $this->render($data);
}

// or customize the layout use
public function index()
{
  $data = array();
  // Do stuff here that puts data in the $data array
  $this->render($data, 'two_col_left');
}



RE: Basic Page Function - puppy - 11-04-2014

(10-25-2014, 06:35 AM)jlp Wrote:
(10-22-2014, 12:48 AM)Tux Wrote: Load "header", "footer" and "content" all at same time by using simple "page()" function.

Site.php Model:
Code:
<?php defined('BASEPATH') OR exit('Access Denied');

class Site extends CI_Model {

    function page($page, $data = NULL) {
        $data['base'] = base_url();
        $data['page'] = current_url();
        $this->load->view('header', $data);
        $this->load->view($page, $data);
        $this->load->view('footer', $data);
    } #page

} #class

#EOF: ./application/models/Site.php

Speaking as an educator, I see MVC as a design pattern to separate state from presentation from control, and I encourage my students not to mix things up.

For instance, if my students submitted work where one of their models loaded any view stuff, they would be penalized marks. Similarly, if they submitted work where a view component accessed a database, they would be penalized marks too.

To me, the proper place for the "page" method you show is in the controller. If this were something used frequently, I would go so far as to put it in a base controller (core/MY_Controller).

Just trying to encourage good practices!

I tend to agree with this. Why use the MVC method if it is not going to be followed? What it comes down to for me is clean code design. Your "Page" function is something I would put in my base controller.

This is certainly an excellent idea (and the same that I use), but in my opinion it is simply in the wrong place.