0

My template parser looks like this (p/s the .'/'. is for readability):

$this->parser->parse($this->settings['theme'].'/'.'header', $data);
$this->parser->parse($this->settings['theme'].'/'.'register', $data);
$this->parser->parse($this->settings['theme'].'/'.'footer', $data);

I don't want to declare $this->parser->parse($this->settings['theme'].'/'.'header', $data); and $this->parser->parse($this->settings['theme'].'/'.'footer', $data); every time in my controller's functions.

How can I extend the MY_Parser.php so that I could use it like this instead:

$this->parser->parse($this->settings['theme'].'/'.'register', $data); will include the register.php between my header.php and footer.php automatically.

The benefit of doing this is to save 2 lines and if I have 20 functions, I can save 40 lines.

Rangka Kacang
  • 327
  • 1
  • 5
  • 12

2 Answers2

1

Just create a function (can be a helper, library extension or model):

function tpl($view, $data) {
    $this->parser->parse($this->settings['theme'].'/'.'header', $data);
    $this->parser->parse($this->settings['theme'].'/'.$view, $data);
    $this->parser->parse($this->settings['theme'].'/'.'footer', $data);
}

If you want you can extend Parser and make a MY_Parser in the libraries folder and do:

class MY_Parser extends CI_Parser {
    function tpl($view, $data) {
        $this->parse($this->settings['theme'].'/'.'header', $data);
        $this->parse($this->settings['theme'].'/'.$view, $data);
        $this->parse($this->settings['theme'].'/'.'footer', $data);
    }    
}

Usage:

$this->parser->tpl($view, $data);

You could do this using $this->parser->parse() but that would require more code as you overwriting the default method and it's just as easy to introduce a new method.

UPDATE:

Using the MY_Parser method you might have to access $this->settings via $this->CI->settings thereby referencing the CI instance in CI_Parser depending on where this variable is coming from.

Alex
  • 9,215
  • 8
  • 39
  • 82
  • Thanks, this is exactly what I was trying to achieve. Right now, I'm creating a function `$this->render('register');` in MY_Controller (which extends CI_Controller) where register is the file to include and in that `render();` function automatically includes the header and footer + data to parse. Is this better than extending the CI_Parser? – Rangka Kacang Mar 21 '18 at 08:24
  • I tend to keep only setup logic and variables in my controller. I think extending parser is fine. – Alex Mar 21 '18 at 17:33
0

Create class with the name of your prefix class name in application/core folder and follow below code. $this->input->is_ajax_request() will only load view other then header and footer if request is from ajax. and in each controller you need to extend YOUR-PREFIX_Controller instead of CI_Controller

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class YOUR-PREFIX_Controller extends CI_Controller {

    protected $header_data;
    protected $footer_data;
    protected $header_view;
    protected $footer_view;

    public function __construct() {
        parent::__construct();
        $this->header_view = 'path-to-header';
        $this->footer_view = 'path-to-footer';
    }

    public function _output($output) {
        if ($this->input->is_ajax_request()) {
            echo ($output);
        } else {
            echo $this->load->view($this->header_view, $this->header_data, true);
            echo ($output);
            echo $this->load->view($this->footer_view, $this->footer_data, true);
        }

    }

}
?>
Farhan
  • 1,453
  • 2
  • 15
  • 20
  • I'm currently using template parser to parse `{title}` for example. Why isn't there any reference on `$this->parser->parse` and this method is using ajax? How do I call your function? Thanks! – Rangka Kacang Mar 20 '18 at 23:17
  • if you want to load only view through ajax in popup this will load only view file and this will ignore header and footer file. you just need to extend this class instead of CI_Controller in each of your controller – Farhan Mar 21 '18 at 01:14
  • replace YOUR-PREFIX_ with subclass_prefix which you have defined in application/config/config.php – Farhan Mar 21 '18 at 01:15
  • I don't think this is actually what I was looking for but I can try to change `$this->load->view` to `$this->parser->parse` to see if the data is parseable. I get logic, now... – Rangka Kacang Mar 21 '18 at 01:50
  • if this will not work please comment i will delete this answer so anyone else will not be confused – Farhan Mar 21 '18 at 02:11
  • I've found a workaround. on my controller, I call a function `$this->render('register');` which extends `MY_Controller` (which extends CI_Controller) and the function goes like `public function render($filename) { $this->parser->parse('header',$data); $this->parser->parse($filename,$data); $this->parser->parse('footer',$data); }` ----- what do you think? – Rangka Kacang Mar 21 '18 at 08:18
  • actually this is what i am referring to https://stackoverflow.com/questions/9540576/header-and-footer-in-codeigniter – Farhan Mar 21 '18 at 08:52