PHP syntax highlighting for .html files in Brackets.io Editor

Sam Stevens picture Sam Stevens · Feb 5, 2014 · Viewed 10.2k times · Source

I work with a templating engine which is powered by .html template files. These templates also allow the use of PHP. Is it possible to enable PHP syntax highlighting for .html files?

Update: As of Sprint 39, Brackets now allows you to include a ".brackets.json" file in your project directory which lets you map file extensions to languages.

This will let you use php syntax highlighting within a html file.

{
    "language.fileExtensions": {
        "html": "php"
    }
}

Answer

peterflynn picture peterflynn · Feb 6, 2014

Update 2: As of Brackets 0.42, this is even simpler still:

  1. Open an .html file
  2. Change the status bar dropdown from HTML to PHP
  3. Open the dropdown again and click "Set as Default for .html Files"

Earlier update: You can now do this via a simple Brackets extension, without needing to modify the core code:

define(function (require, exports, module) {
    var LanguageManager = brackets.getModule("language/LanguageManager");
    var htmlLang = LanguageManager.getLanguage("html"),
        phpLang  = LanguageManager.getLanguage("php");
    htmlLang.removeFileExtension("html");
    phpLang.addFileExtension("html");
});
  1. Put this code in a file named main.js
  2. In Brackets, go to Help > Show Extensions Folder
  3. Create a new folder under user, and place the main.js file inside it
  4. Restart Brackets

Here's more info on writing Brackets extensions, if needed.


(old answer)

Unfortunately this is tricky in Brackets currently. Syntax highlighting is extensible in that you can assign new file extensions to a particular highlighting mode, but the ".html" extension is already in use by the plain-HTML highlighting mode.

Your best options are probably:

a) Pull Brackets from git and locally patch the languages.json file so that .html is interpreted as PHP instead. (You could do this in a regular installed build but it won't be fun, since that JSON file is baked into a giant minified blob). Running from a git copy isn't too hard though.

b) Rename all your files to reflect that they're not plain HTML. Brackets will automatically recognize ".phtml" for example, which seems like a more appropriate file extension anyway. (If there's some other extension that your templating system will use but Brackets doesn't recognize automatically, you can use this trick to register it as a PHP-type extension).