Hello I just started working with CodeIgniter framework. My current directory structure is
Demo(Project name) +System +Application -Controllers demo.php +Model -Views view_demo.php -Js ajax.js jquery.js
Please tell me how to include .js files in view_demo.php.
You need to use the
So, in the view_demo.php file:
You will need the URL helper loaded. To load the helper you need to put on your demo.php controller:
You can also autoload on \config\autoload.php on the helpers array.
More info about base_url(): http://www.codeigniter.com/user_guide/helpers/url_helper.html#base_url
You wouldn’t include JS files within the PHP, they would be output as script tags within the HTML you produce which you may be producing as output from the PHP script.
As far as I know, there is no built in CodeIginiter function to include this output like there is for CSS using the
link_tag() function provided by CI. I’ve added a function called
script_tag() to the
system/helpers/html_helper.php file from CI. The function is:
Then in your PHP code you can do:
Another technique I adopted was to define an array of scripts to include in my controller, then loop through the array in my view to include them. This allows me to include specialty js functions only when needed.
$data['scripts to load'] = array('edit.js','menu.js', 'contact.js'); $this->load->view('myview');
Then in the view
If you have script files that get loaded on every page, you can hard code them in your footer view like is described in the other answers.
Check out Phil Sturgeon’s CodeIgniter Template Library. We use a modified version of it at work.
Just use the standard:
inside your view! (Not inside the PHP tags, of course.) I don’t think the CodeIgniter HTML helper has any functions that you could use as an alternative to writing out the HTML yourself.