Load template automatically

Asked

Viewed 1,111 times

6

It is possible to program the codeIgniter to load the template automatically without specifying the views at each call in the controller?

Example

user controller

public function listar() {

        $this->load->view('template/header.php');
        $this->load->view('template/navbar.php');
        $this->load->view('template/principal.php');
        $this->load->view('template/footer.php');
}

sales controller

public function listar() {

        $this->load->view('template/header.php');
        $this->load->view('template/navbar.php');
        $this->load->view('template/principal.php');
        $this->load->view('template/produtos.php');
        $this->load->view('template/footer.php');
}

It is noticed that there is a code repetition every call. I want to know if there is a way to call the views header, navbar and automatic footer.

3 answers

5


Using the Most Simple Template Library for Codeigniter

To Most Simple Template Library for Codeigniter is a really quite simple library to use. Normally, the views are loaded as follows:

$this->load->view('conteudo', $data);

The code above means: loading the "content" view.

Using the template system, the new way is:

$this->template->load('template', 'conteudo', $data);

That is: loading the "content" view inside the "template" template.

Installation

Install the template system through these simple steps:

  1. Download the file ci_template_library.zip;

  2. Put the Template.php file into application/Libraries;

  3. Autoload the library into the file application/config/autoload.php: $autoload['libraries'] = array('template');;

Use of the Most Simple Template Library

First, you need to create the template that will be used. For example:

application/views/template.php:

<html>
    <body>
        <div id="contents"><?php echo $contents ?></div>
        <footer>Copyleft</footer>
    </body>
</html>

Note the fact that the view (with the data) will always be grafted in the place where, in the template, has the variable $contents (In the example, within <?php echo $contents ?>).

Let’s take the example of a "content" view:

application/views/content.php:

<header><h1>Nome do site</h1></header>
<p>Algum par&aacute;grafo com conte&uacute;dos.</p>

And finally, at the point of the controller where we normally load the view, we now load the template, using its own syntax:

$this->template->load('template', 'conteudo');

5

I also did otherwise very simple using a Helper:

template_helper.php

<?php

function teste($pagina){
    $ci = get_instance();

    $ci->load->view('admin/static/header');
    $ci->load->view($pagina);
    $ci->load->view('admin/static/footer');
}

Controller I am using

class Dashboard extends CI_Controller {

        public function index()
        {
            $this->load->helper('template');
            teste('admin/dashboard');
        }

}

I pass the page I want to render as a parameter of the Helper function: 'admin/dashboard'

1

Without the use of third-party libraries it is also possible to create a template system easily. I did it as follows:

First you need to define a controller-base in the file /Application/Core/My_controller.php. In this file I will define a method called view().

 <?php 
defined('BASEPATH') OR exit('Acesso Negado');

 class BaseController extends CI_Controller {

    public function view($arquivo, $titulo, $dados = NULL) {
      $dados = ["arquivo" => $arquivo,
                "titulo" => $titulo,
                "dados" => $dados
              ];

      $this->load->view("template.php", $dados);
    }
 }

 ?>

Create the template.php file in the folder /Application/Views/

<html>
  <head>.....</head>
  <body>
     <?php $this->load->view("header.php"); ?>

     <div class="main container">

       <?php
           $this->load->view($arquivo, $dados);
         ?>
    </div>

     <?php $this->load->view("footer.php"); ?>
 </body>
</html>

Example of use

class Cursos extends BaseController {

  public function index() {
      $this->load->model("CursoModel", "model");
      $dados = ["cursos" => $this->model->listar()];
      $this->view("cursos/cursos.php", "Listagem de Cursos", $dados);
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.