How to make custom helper work. |
[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'); 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');
[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'); I would like someone with more experience than myself to leave post here and say if I am wrong.
[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'); 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.
[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'); 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'); 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 ). edit: I fixed quote signs in MY_Controller.php code
[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'); 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'); 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 ). edit: I fixed quote signs in MY_Controller.php code[/quote] tried that way did not work. Only echoed out errors.
[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 Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); common/home.tpl Default Route This is sample of opencart/ system/engine/controller Code: protected function render() { Sample Of OpenCart Template Library Code: <?php
[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: ~~ Code: <?php $header; ?> 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.
[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: ~~ Code: <?php $header; ?> 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.
[eluser]noideawhattotypehere[/eluser]
application/core/MY_Loader.php Code: class MY_Loader extends CI_Loader { usage Code: $views = array('header', 'column_left', 'footer'); |
Welcome Guest, Not a member yet? Register Sign In |