0

I'am probably missing some knowledges in PHP, and seems can't get it work right.

I have a application/config/cfg.backend.php with this values:

$config['head_meta']            = array(
    'stylesheets'               => array(
        'template.css'
    ),
    'scripts'                   => array(
        'plugins/jquery-2.0.3.min.js',
        'plugins/bootstrap.min.js'
    ),
    'end_scripts'               => array(
        'plugins/jquery-ui.js',
        'plugins/jquery.dataTables.min.js',
        'template.js'
    )
);

There I load all necessary scripts and css files, so whenever I will need to extend some of these arrays, I will simply use an array_push() function like I did in my application/controllers/backend/Categories.php:

class Categories extends Backend_Controller{

    function __construct(){

        parent::__construct();

        // Load dependencies
        $head_meta = config_item('head_meta');
        array_push($head_meta['end_scripts'], 'plugins/redactor.min.js', 'categories.js');
        array_push($head_meta['stylesheets'], 'redactor.css');
        var_dump($head_meta['end_scripts']);
    }

    // THE REST OF THE CLASS ...
}

So by doing a var_dump($head_meta['end_scripts']), I see that the array_push() did his job but my scripts weren't loaded, and I don't know why, I'am stucked here.

array (size=5)
  0 => string 'plugins/jquery-ui.js' (length=20)
  1 => string 'plugins/jquery.dataTables.min.js' (length=32)
  2 => string 'template.js' (length=11)
  3 => string 'plugins/redactor.min.js' (length=23)
  4 => string 'categories.js' (length=13)

Any suggestions what I'am doing wrong ?

==== UPDATED ====

I have a main template file located in applications/views/backend/templates/template.php where at the bottom of the page I do a foreach() to load end_scripts:

<?php foreach($this->config->item('end_scripts', 'head_meta') as $end_scripts):?>
    <script src="<?php echo base_url();?>assets/js/<?php echo $end_scripts;?>" type="text/javascript"></script>
<?php endforeach;?>

And to load a specific view into the main templates/template.php, I do this:

    // Insert catched data into the component view of main template
    $data['component'] = $this->load->view('backend/category_list', $componentData, TRUE);

    // Load a component view into the main template
    $this->load->view('backend/templates/template', $data);
4
  • Are you actually printing the script tags in the view? Commented Jul 8, 2013 at 14:17
  • what you see when you see the rendered source code on browser or in firebug? are your paths correct ? Commented Jul 8, 2013 at 14:18
  • @BadWolf - I updated the question, pls refer to the updated section. Commented Jul 8, 2013 at 14:22
  • @user1191081 - The strange behavior is that a var_dump() is doing before the whole HTML source code (before where <html>... starts), but at the end of the file, there is no new values in an array I've pushed before, like it's not even populated. Commented Jul 8, 2013 at 14:24

1 Answer 1

1

The Codeigniter Config class loads the config variables inside the class making them immutable without calling $this->config->set_item(). To add variables to an array in the config class from your controller, you need to modify the array, and then set the variables back to the config class before they can be accessed from elsewhere in the program.

class Categories extends Backend_Controller{

    function __construct(){

        parent::__construct();

        // Load dependencies
        $head_meta = config_item('head_meta');
        array_push($head_meta['end_scripts'], 'plugins/redactor.min.js', 'categories.js');
        array_push($head_meta['stylesheets'], 'redactor.css');
        $this->config->set_item('head_meta', $head_meta);
        var_dump($this->config->get_item('head_meta'));
    }

    // THE REST OF THE CLASS ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I see now, thank you for clarify the things, I didn't know about that the class vars are locked for override except it's own functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.