问题
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"
}
}
回答1:
Update 2: As of Brackets 0.42, this is even simpler still:
- Open an .html file
- Change the status bar dropdown from HTML to PHP
- 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");
});
- Put this code in a file named main.js
- In Brackets, go to Help > Show Extensions Folder
- Create a new folder under
user
, and place the main.js file inside it - 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).
来源:https://stackoverflow.com/questions/21569932/php-syntax-highlighting-for-html-files-in-brackets-io-editor