Codeigniter - loading a specific JS library in a specific form

I am trying to download google API, i.e.

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

in my template. But since I have only one page with a google map (I would prefer not to have an API download for all files), how do I send a message from the controller to the view that I want to download the JS file for this?

Thank you for your help.

+1
source share
5 answers

CodeIgniter has a segment class. You could run some code, for example:

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

When a script is loaded on the page http://yoursite.com/map/ .

+4
source

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 do not want to use the template library, follow these steps:

* provided that on the controller "Map", and you need a 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 opinion:

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

essentially, you create an array of files "> w370> files / css (independently), then check for its presence and unload it in the head section of your view.

I personally would choose a template.

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

+4
source
 <?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 to your helper_directory under the name head_helper. In your controller inside the action use this code

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

And in your view file use this function

 headscripts($headscripts); 

For style sheet use

 headlinks($headlinks); 

And yes, don't forget to download the helper using the autoload.php file in the config folder, like this

 $autoload['helper'] = array('url', 'file','head'); 
+1
source

Thanks for your answers, guys, I finished work on Ross and leaf suggestions before I found your answers here, so I think I was on the right track.

My controller has:

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

and my opinion has:

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

and my β€œspecific” look has what it takes.

That way, I can load as many user scripts as possible, and also have code in the view, not the controller.

Thanks again, but keep offering further suggestions!

0
source

write for this assistant. Pass the names of the scripts in the array and inside the helper function to iterate over them and print the scripts

-2
source

Source: https://habr.com/ru/post/1491325/


All Articles