1

I'm trying to load the google maps API ie:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">

in my head template. But because I've only got one page with a google map on it (I'd rather not have the API load for all files), how would I send the message from the controller through to the view that I wish to load this particular JS file?

Thanks for your help.

DumKopf
  • 11
  • 1
  • 2

5 Answers5

4

CodeIgniter has a segments class. You would be able to run some code like:

<?php if($this->uri->segment(1) == 'map') { ?>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">
<?php } ?>

When on page http://yoursite.com/map/ it will load the script.

Jo Albright
  • 560
  • 1
  • 5
  • 14
  • That is a very good option, but I guess I don't want to get into the situation of having a heap of "if statements" depending on which page the user is on. But thanks for your solution, I'll use that unless a better approach is suggested. – DumKopf Feb 04 '11 at 07:53
  • The only other way to specify it to one page is by attaching it to a view that will only be loaded on that page. – Jo Albright Feb 04 '11 at 07:57
4

One solution is to either use a template library that has javascript/css "injection" - see:

http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html#utilities

$this->template->add_js('js/jquery.js');
$this->template->add_js('alert("Hello!");', 'embed');

for more information.

If you don't want to use a template library, do something like this:

*assuming on the "Map" controller, and that you need the JS file on the default page.

class Map extends CI_Controller {

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

    function index()
    {
        $scripts = array(
    '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">' . "\n", 
    '<script>something</script>');
       /* this method lets you add multiple...*/

        $data['scripts'] = $scripts;
        $this->load->view('my_map/index', $data);
    }
}

in your view:

if(isset($scripts))
{
    foreach($scripts as $script)
    {
        echo $script;
    }
}

essentially you build an array of script files/css files (whatever), then check for its prescence and dump it in the head section of your view.

I'd personally go for the template option.

Also note CI2.0 has a new javascript driver might be worth a read

Ross
  • 18,117
  • 7
  • 44
  • 64
  • +1 for this suggestion. I've done a similar approach to this, but by having the configuration map controllers to the javascript path includes, that way everything is configured separately from the controller, and the controller does not need to worry about creating arrays of string includes. Let a library or view create the actual output of the markup script tags, not the controller. One place to change, instead of hunting through a bunch of controller files when a script api changes version. – cweston Feb 04 '11 at 13:33
  • Javascript Driver link seems to be broken? – Jo Albright Feb 04 '11 at 15:17
1
<?php

/**
 * Head files loader
 * @author azhar
 **/

function headscripts($path)
{
    if(is_string($path))
    {
        echo "<script type='text/javascript' src='". base_url($path) ."'></script>\n";
    }elseif(is_array ($path)){
        foreach ($path as $p) {
            echo "<script type='text/javascript' src='". base_url($p) ."'></script>\n";
        }
    }
}

function headlinks($path)
{
    if(is_string($path))
    {
        echo "<link rel='stylesheet' href='". base_url($path) ."'/>\n";
    }elseif(is_array ($path)){
        foreach ($path as $p) {
            echo "<link rel='stylesheet' href='". base_url($p) ."'/>\n";
        }
    }
}
?>

Add this file in your helper_directory under the name head_helper. In your controller inside an action use this code

$data['headscripts'] = array('js/main.js');

And in your view file use this function

headscripts($headscripts);

For stylesheet use this

headlinks($headlinks);

And yes do not forget to load the helper using autoload.php file in config folder like this

$autoload['helper'] = array('url', 'file','head');
Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30
0

Thanks for your answers guys, I ended up doing a mixture of Ross and leaf dev's suggestions before I found your answers here, so I guess I was on the right track.

My controller has:

$data['head'] = array('specificjs');
$this->load->view('view',$data);`

and my view has:

if(isset($head)){
    foreach($head as $item){
        $this->load->view('js/'.$item);
    }
}

and my 'specificjs' view has what's required.

This way I can load as many custom scripts as I want and have the code in a view not a controller.

Thanks again, but keep any further suggestions coming!

  • I am in a similar situation. How did you specify the link to "specificjs". I have my jquery files in a separate scripts folder. – Faiyet Jul 11 '13 at 17:42
-2

write a helper for this. Pass the scripts names in an array and inside the helper function iterate over them and print the scripts