API Development

TiPHP & TiPython

Titanium Mobile development is done using JavaScript and with the Titanium module platform developers can also build custom modules using Objective-C and/or Java as well. I’ve always liked the fact that with Titanium Desktop developers also have access to other programming languages like Python, PHP, and Ruby. Last week in discussions with a few other team members here at Appcelerator, we came with a few experimental ideas that explore bringing PHP and Python into the mobile platform as well, sort of…

TiPHP

Many of you probably know of an open source project by the name of php.js. The goal of this project is to port common PHP functions to JavaScript. The end result is in fact JavaScript, but the functions look and behave like functions developers are use from the PHP world. In TiPHP we took the full namespaced version of php.js found here and created a JavaScript only Titanium module with it. An instance of PHP_JS is created and injected into the Ti namespace as Ti.PHP. From that point forward a large number of common PHP functions can be called directly off the Ti.PHP namespace (e.g.) Ti.PHP.in_array(). The project also allows for custom builds so while we started with the full default set of functions you could actually get as granular as you want.

TiPython

With TiPython we took a slightly different approach. We ended up embedding Skulpt, an open source in-browser implementation of Python, into a Titanium Module. Again this is a simple JavaScript only module. The advantage of this over TiPHP is that, in addition to gaining access to many of the core language functions, we can actually execute Python statements inline and/or interact with pre-existing Python classes stored in external files.

Skulpt’s “Sk” object is aliased as Ti.Python. The syntax for Skulpt takes some getting use to. We’ve written a few basic wrapper functions to make interacting with it a bit easier, but this could be greatly improved. Ideally these helper functions would be set in the module itself, but we ran into some difficulty exporting the newly added methods into the Skulpt object via the module include (Note: it does work via a standard Ti.include though). For now we’ve broken these methods out and include them separately after the module has loaded.

Another path worth exploring down the line would be to utilize the emscripten project which would allow us to convert all of CPython to JavaScript via LLVM.

TiRuby

We didn’t get around to experimenting with Ruby this time around. Maybe someone else will. There is an open source Ruby.js project available. It appears to be fairly old, but still probably a decent starting point. The major difference here is that it modifies core JavaScript objects much like the Prototype JavaScript library. Therefore, you could simply include the JavaScript file in Titanium and make use of it immediately, but we don’t generally recommend modifying the JavaScript core. Proper namespacing is a recommended best practice.

Grab and Go

We had fun experimenting with these ideas and we are open sourcing both modules and two very simple demo apps so that you can experiment with them as well. To run the samples you will need to build the modules and place them in the corresponding demo apps directory. The modules are not currently working for Android, but the demo apps do include a temporary workaround (basically just including the standalone JavaScript files via Ti.include) that allows the apps to run in the Android emulator and/or device.