CodeIgniter Forums

Full Version: How to make custom helper work.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]razerone[/eluser]
Hi. Currently I am arraying some of my views but its to long I would like to be able to make a helper that would load the view in array so all I would have to do is $data = array (header); rather than $data = array (header => $this->load->view(theme/default/template/common/header.tpl)); that way is to long.

Here is what it looks like in my controller home.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

public function index() {

$this->children = array(
'header' => $this->load->view('theme/default/template/common/header.tpl'),
'column_left' => $this->load->view('theme/default/template/common/column_left.tpl'),
'column_right' =>  $this->load->view('theme/default/template/common/column_right.tpl'),
'content_top' => $this->load->view('theme/default/template/common/content_top.tpl'),
'content_bottom' => $this->load->view('theme/default/template/common/content_bottom.tpl'),
'footer' => $this->load->view('theme/default/template/common/footer.tpl')
);
$this->load->view('theme/default/template/common/home.tpl' , $this->children);
}
}

Would like to be able to do it this way just don't know how to word it in helper file. "template"

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

class Home extends CI_Controller {

public function index() {

$this->children = array(
'header',
'column_left',
'column_right',
'content_top',
'content_bottom,
'footer'
);
$this->load->view('theme/default/template/common/home.tpl' , $this->children);
}
}

El Forum

[eluser]Tpojka[/eluser]
You have somewhere to define those elements. For example, in first code you've posted above, you defined
header as $this->load->view('theme/default/template/common/header.tpl'),
so it is clearly to interpreter to use header value. If you want to override native Loader function, I've seen already in some starting packages, such as skeleton too (find it on github typing 'codeigniter skeleton'), there are included such files in core folder.
Anyway, you need to override view method from Loader.php file, especially $vars array parameter of method and the way how method is reding it. Method is on line 417.
So you can make your class in application/core folder with custom overriding method.

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

class MY_Loader extends CI_Loader
{
  public function construct__()
  {
    parent::__construct();
  }

  public function view($view, $vars = array(), $return = FALSE)
  {/* you have to change this part to response your needs : this is copied function from Loader.php
    return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));*/
  }
}

I would like someone with more experience than myself to leave post here and say if I am wrong.

El Forum

[eluser]razerone[/eluser]
[quote author="Tpojka" date="1390146085"]You have somewhere to define those elements. For example, in first code you've posted above, you defined
header as $this->load->view('theme/default/template/common/header.tpl'),
so it is clearly to interpreter to use header value. If you want to override native Loader function, I've seen already in some starting packages, such as skeleton too (find it on github typing 'codeigniter skeleton'), there are included such files in core folder.
Anyway, you need to override view method from Loader.php file, especially $vars array parameter of method and the way how method is reding it. Method is on line 417.
So you can make your class in application/core folder with custom overriding method.

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

class MY_Loader extends CI_Loader
{
  public function construct__()
  {
    parent::__construct();
  }

  public function view($view, $vars = array(), $return = FALSE)
  {/* you have to change this part to response your needs : this is copied function from Loader.php
    return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));*/
  }
}

I would like someone with more experience than myself to leave post here and say if I am wrong.[/quote]

With my arrays the top example is working but only want to be able to put example 'header' rather than long way just like open cart template engine.

El Forum

[eluser]Tpojka[/eluser]
You can try something such as CI native using of third parameter set to true in
$this->load->view('template', $arr, true);
function of your controller method.
Check documentation (last line).
Also, you can define it in APPPATH . '/core/' . MY_Controller.php.
After that you can use it in any controller that is extending MY_Controller:
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{

  public $header = '$this->load->view(\'theme/default/template/common/header.tpl\')';
  public $column_left = '$this->load->view(\'theme/default/template/common/column_left.tpl\')';

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

}

Now in your controller you can use variables $this->header and $this->column_left.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// extending MY_Controller instead native one is important
class Home extends MY_Controller
{

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

  public function index()
  {
    $this->children = array(
      $this->header,
      $this->column_left
    );

    $this->load->view('theme/default/template/common/home.tpl' , $this->children);
  }

}
You have to define value of what you want to be output, somewhere.
You gave answer to yourself already: if you want to get some data(output, view, what else) typing header, you have to define somewhere in code what header should return.
PHP nor CI don't read minds (yet Tongue ).

edit: I fixed quote signs in MY_Controller.php code

El Forum

[eluser]razerone[/eluser]
[quote author="Tpojka" date="1390152364"]You can try something such as CI native using of third parameter set to true in
$this->load->view('template', $arr, true);
function of your controller method.
Check documentation (last line).
Also, you can define it in APPPATH . '/core/' . MY_Controller.php.
After that you can use it in any controller that is extending MY_Controller:
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{

  public $header = '$this->load->view(\'theme/default/template/common/header.tpl\')';
  public $column_left = '$this->load->view(\'theme/default/template/common/column_left.tpl\')';

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

}

Now in your controller you can use variables $this->header and $this->column_left.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// extending MY_Controller instead native one is important
class Home extends MY_Controller
{

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

  public function index()
  {
    $this->children = array(
      $this->header,
      $this->column_left
    );

    $this->load->view('theme/default/template/common/home.tpl' , $this->children);
  }

}
You have to define value of what you want to be output, somewhere.
You gave answer to yourself already: if you want to get some data(output, view, what else) typing header, you have to define somewhere in code what header should return.
PHP nor CI don't read minds (yet Tongue ).

edit: I fixed quote signs in MY_Controller.php code[/quote]

tried that way did not work. Only echoed out errors.

El Forum

[eluser]Tpojka[/eluser]
What did the error say?

El Forum

[eluser]razerone[/eluser]
[quote author="Tpojka" date="1390210647"]What did the error say?[/quote]

It was on views.

I though I would try different way now.

for each of my common views I have made a controller.



Code:
controllers
common/header.php <-- Header View Loaded in there
common/column_left.php <-- Column_left View Loaded in there
common/column_right.php <-- Column_right View Loaded in there
common/content_top.php <-- Content_top View Loaded in there
common/content_bottom.php <-- Content_bottom View Loaded in there
common/footer.php <-- Footer View Loaded in there
common/home.php Default Route

view
common/header.tpl
common/column_left.tpl
common/column_right.tpl
common/content_top.tpl
common/content_bottom.tpl
common/footer.tpl

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

public function __construct()
       {
            parent::__construct();
            $this->load->helper('array');
       }

public function index() {
  $this->children = array(
   'header',
   'column_left' ,
   'column_right',
   'content_top',
   'content_bottom',
   'footer'
  );

  $this->load->view('theme/default/template/common/home.tpl' , $this->children);

}
}


common/home.tpl Default Route

This is sample of opencart/ system/engine/controller
Code:
protected function render() {
foreach ($this->children as $child) {
$this->data[basename($child)] = $this->getChild($child);
}
if (file_exists(DIR_TEMPLATE . $this->template)) {
extract($this->data);
ob_start();
require(DIR_TEMPLATE . $this->template);
$this->output = ob_get_contents();
ob_end_clean();
return $this->output;
} else {
trigger_error('Error: Could not load template ' . DIR_TEMPLATE . $this->template . '!');
exit();    
}
}

Sample Of OpenCart Template Library

Code:
&lt;?php
class Template {
public $data = array();

public function fetch($filename) {
  $file = DIR_TEMPLATE . $filename;
    
  if (file_exists($file)) {
   extract($this->data);
  
        ob_start();
      
     include($file);
      
     $content = ob_get_contents();

        ob_end_clean();

        return $content;
     } else {
   trigger_error('Error: Could not load template ' . $file . '!');
   exit();    
     }
}
}
?&gt;

El Forum

[eluser]Tpojka[/eluser]
Opencart code can't help you much if you don't understand CI code yet.
You have to start from point that works and that is cide from first post.
Than you need to rationalize your code as well.
Other way is to write some class/library from scratch.
If all view files are in same folder, you can make variables that include each file.
Controller:
Code:
~~
$this->children['header'] = include('header.tpl');
$this->children['left_column'] = include('left_column.tpl');

$this->load->view('home.tpl', $this->children);
~~
View - home.tpl:
Code:
&lt;?php $header; ?&gt;
&lt;?php $left_column; ?&gt;

But I would suggest you the way you already know and when you are finish, than you retionalize or change this finesses.

I am looking again at this. You already have working code.
For that you want in first post, you will need to write your own version of template library, I think.

El Forum

[eluser]razerone[/eluser]
[quote author="Tpojka" date="1390213360"]Opencart code can't help you much if you don't understand CI code yet.
You have to start from point that works and that is cide from first post.
Than you need to rationalize your code as well.
Other way is to write some class/library from scratch.
If all view files are in same folder, you can make variables that include each file.
Controller:
Code:
~~
$this->children['header'] = include('header.tpl');
$this->children['left_column'] = include('left_column.tpl');

$this->load->view('home.tpl', $this->children);
~~
View - home.tpl:
Code:
&lt;?php $header; ?&gt;
&lt;?php $left_column; ?&gt;

But I would suggest you the way you already know and when you are finish, than you retionalize or change this finesses.

I am looking again at this. You already have working code.
For that you want in first post, you will need to write your own version of template library, I think.[/quote]

Ok will keep playing around with the code see what happens.

El Forum

[eluser]noideawhattotypehere[/eluser]
application/core/MY_Loader.php
Code:
class MY_Loader extends CI_Loader {

    public function __construct() {
        parent::__construct();
    }
    
    function array_to_view($views) {
        foreach ($views as $single_view) {
            parent::view('theme/default/template/common/'.$single_view.'.tpl');
        }
    }

}

usage
Code:
$views = array('header', 'column_left', 'footer');
$this->load->array_to_view($views);
Pages: 1 2