How to use CodeIgniter config values inside Mootools

I am using Mootools with most of my CodeIgniter projects, and I often have to use site_url() and base_url() inside my Mootools script especially when I’m dealing with ajax uploading (in order to refresh picture thumbnails for example).

That’s why I have implemented a very simple way to use CodeIgniter‘s config values in a Mootools script :

Setting up the CodeIgniter controller

The point is to generate a JavaScript file with a CodeIgniter controller.

  1. Create a new PHP file called mootools.php in system/application/controllers/
  2. Copy / Paste the code below inside mootools.php

Adding the JavaScript file to your header

Next, we have to add the JavaScript file generated by CI inside our HTML header, just add this line after the mootools inclusion in your header view:

Testing

Here is a simple testing script that write your base_url() and site_url() value in two H2 tags.

The CodeIgniter view to load:

The JavaScript testing file:

  1. Create a new file in /js called demo.js
  2. Copy / Paste the code below inside demo.js

Then just load the view inside a controller to see your config values printed in the two H2 tags 😉

PS: Don’t forget to add the mootools core in the /js folder !

Une réflexion au sujet de « How to use CodeIgniter config values inside Mootools »

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *