Upload template automatically

4

Is it possible to program codeIgniter to load the template automatically without having to specify the views for each call in the controler?

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 for each call. I want to know if there is a way to call the header, navbar, and automatic footer views.

    
asked by anonymous 17.11.2014 / 19:18

3 answers

3

Using the Most Simple Template Library for CodeIgniter

The Most Simple Template Library for CodeIgniter is a really simple library to use. Normally, you load the views as follows:

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

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

Using the template system, the new way is:

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

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

Installation

Install the template system through these simple steps:

  • Download the file ci_template_library .zip ;

  • Place the Template.php file in application / libraries ;

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

  • Using 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>
    

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

    Take the example of a "content" view:

    application / views / conteudo.php :

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

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

    $this->template->load('template', 'conteudo');
    
        
    20.11.2014 / 17:54
    4

    I also did it in a very simple way 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'm using

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

    Step to the page that I want to render as Helper function parameter: 'admin/dashboard'

        
    20.11.2014 / 20:56
    0

    Without using third-party libraries it is also possible to easily create a template system. I did this:

    First you need to define a controller-base in the /Application/Core/MY_Controller.php file. 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 / Application / Views / folder

    <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 usage

    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);
      }
    }
    
        
    02.02.2017 / 11:38