How best to use templates in Codeigniter 3

Asked

Viewed 1,407 times

0

I am using version 3 of codeigniter and inserting the concept of templates through the PARSER library, according to the CI documentation itself

https://www.codeigniter.com/userguide3/libraries/parser.html

It turns out that in version 2 I use a layout model through a Hook, as this example:

http://flaviosilveira.com/2010/habilitando-layouts-no-codeigniter-template-engine-2/

In the latter the use was simpler because in Hook I already had the definition of which part of the block would the content of my view, ie making the includes of the Static parts, such as header, and footer, changing only the view body.

Already in the case in the parser I still could not understand how to make these calls of includes staticos more usual.

It would be something like:

class Home extends CI_Controller {

       public function index() {     
            $data = array(
                       'blog_title' => 'My Blog Title',
                        'title' => 'My Title'
                     );
            $row['nome'] = $this->session->userdata('nome');
            $this->parser->parse('templates/cabecalho', $row);
            $this->load->view('welcome_message');
            $this->parser->parse('templates/rodape');
        }
}

This would be the best model to work with layouts using PARSER?

  • The best way would be for the template engine to provide some mechanism to 'assemble' the layout. The code shown in the question seems Boilerplate imagine repeat the calls parse(), view(), parse() (roughly there are 3 includes in a rigid order) for all pages, imagine if you had a side menu there would be 4 files called all the time, it is possible to create a method to center the mount.

  • The idea would be to have a 'main' file that would have 3 'sessions', header, kernel and footer, in the controller vc would only pass the kernel with the data. You can do this without a framework of a look how the Laravel does is cool.

  • 1

    Exactly this, the repetition of the code would be something extremely tiring and not at all unusual. The blade of the Laravel I think is excellent, and the hook I used in the previous version is very close to that. In case developing a class to make this interaction between the calls of the templates and the insertion of the contents.

  • Then I’m gonna take a look at this hook.

  • Yes, although I would like to adopt the parser in CI3, and get a little closer to the bucket, I still see the parser as not very intuitive, forced to repeat codes, or use the same logic from hook. Anyway thanks for the help

1 answer

1

Particularly I like to use that way:

public function _site($view) {

    $this->load->vars($this->dados);
    $this->load->view('view_topo');      
    if (is_array($view)) {
        foreach ($view as $valor) {
           $this->load->view($valor);
        }
    } else {
        $this->load->view($view);
    }     
    $this->load->view('view_rodape');       
}

In this case, I call on the controller:

    $dados['vantagems'] = $this->model_vantagem->get_vantagem_lista();
    $this->load->vars($dados); // Load nas variaveis que eu preciso usar na view
    $this->_site(array('view_vantagem'));

Maybe it’ll help you too!

  • Andre, in case you implemented a class with this first and snippet and when you extend it in your controller you call the _site($view) method. Now the variable data, Voce can send it to view through a $this->_site(array('view_advantage'), $data)???

  • Exactly, I created a class called my_controller, which I did extends Mx_controller, to be able to better control the site, so I call the views correctly already... if I call just a view, without top or footer, just call $this->load->view("view");

  • Yes, more or less like the hook I mentioned, however the part that Voce puts $this->load->vars($this->data), will never be used in the _site method, it would be necessary to pass it as parameter tbm

Browser other questions tagged

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