Mixing PHP and JavaScript in TextMate

About six months ago I switched to using TextMate as my text editor, after using BBEdit for as long as I can remember. For those of you in civilian life this might seem like a trivial thing; it isn’t. I spend 95% of my working life inside a text editor, so switching is something like trading in a Ford pick-up for a Dodge.

A text editor is a text editor is a text editor. All pick-up trucks have a cab, and a bed, and wheels and an engine. But the cup-holders are in different places, and the tailgate works differently, and the way your program you favourite stations into the radio changes. And so it is with text editors. In other words, when you switch there’s a “getting used to it” period that lasts for months. I’m there, and TextMate feels like home now.

But I’m still learning.

One important thing I learned today is that TextMate and its “bundles” — the language-specific add-ons that make TextMate “know” about PHP, HTML, JavaScript, etc. — are smart enough to understand that a single PHP file can intermix CSS, HTML, PHP and JavaScript code and react accordingly.

The key to all this, which is, alas, buried deep within the TextMate documentation, is to set the language to HTML:

Screen Shot of Language Setting in TextMate

Once you do this, you’ll find that the syntax highlighting, function lookup, and other language-specific features of TextMate work in a language-appropriate way depending on where you are in the document. It’s all really rather magical.

Comments

Daniel Von Fange's picture
Daniel Von Fange on May 19, 2008 - 17:07

It will even pick up SQL in strings, in PHP, in HTML.

Add new comment