You can download this mode here.
A GitHub repository is available: http://github.com/fxbois/web-mode/
web-mode.el is GPL and Free Software.
First drop the file web-mode.el in a directory defined in your load-path.
Then, add in your .emacs
(require 'web-mode)
(add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.jsp\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
Using web-mode for editing plain HTML files can be done this way
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))
(defun web-mode-hook ()
"Hooks for Web mode."
(setq web-mode-markup-indent-offset 2)
)
(add-hook 'web-mode-hook 'web-mode-hook)
(setq web-mode-markup-indent-offset 2)
CSS offset indentation
(setq web-mode-css-indent-offset 2)
Script offset indentation (for JavaScript, Java, PHP, etc.)
(setq web-mode-code-indent-offset 2)
HTML content is not indented by default (indeed indenting the content of a TEXTAREA for example can have nasty side effects).(setq web-mode-indent-style 2)
(setq web-mode-comment-style 2)
(set-face-attribute 'web-mode-css-rule-face nil :foreground "Pink3")
Available faces:(define-key web-mode-map (kbd "C-n") 'web-mode-tag-match)
(add-to-list 'web-mode-snippets '("mydiv" "<div>" "</div>"))
name, beg, end (if region exists, the content is inserted between beg and end)
(setq web-mode-disable-auto-pairing t)
CSS colorization
(setq web-mode-disable-css-colorization t)
Block faces: can be used to set the background of blocks
(see the faces web-mode-server-face, web-mode-css-face, web-mode-javascript-face)
(setq web-mode-enable-block-faces t)
Heredoc (cf. PHP strings) fontification (when the identifier is <<<EOTHTML or
<<<EOTJAVASCRIPT)
(setq web-mode-enable-heredoc-fontification t)
(setq web-mode-extra-php-constants '("constant1" "constant2"))
Also available:
web-mode-extra-php-keywords,
web-mode-extra-javascript-keywords,
web-mode-extra-jsp-keywords,
web-mode-extra-asp-keywords
(setq web-mode-engines-alist
'(("smarty" . "\\.smar\\'")
("blade" . "\\.blade\\.")
("go" . "\\.goml\\'")))
Using this association list is required with some engines when not using the default extension: smarty (*.tpl), velocity/cheetah (*.vm, *.vtl), CTemplate/Mustache (*.mustache), blade (*.blade.php).
General
|
HTML element
|
HTML tag
|
| name | cousins | extensions | example |
|---|---|---|---|
| django | twig, jinja, jinja2, erlydtl | *.djhtml, *twig*, *.dtl, *.tmpl |
{# comment #} <ul id="navigation"> {% for item in items %} <li>{{ item }}</li> {% endfor %} </ul> |
| php | *.php, *.psp, *.ctp |
<?php /* comment */ ?> <ul id="navigation"> <?php foreach ($items as $item): ?> <li><?=$item?></li> <?php endforeach; ?> </ul> |
|
| erb | eruby, ember, erubis | *.erb, *.rhtml | |
| velocity | cheetah | *.vsl, *.vm | |
| blade | *blade* | ||
| go | gtl | *.gohtml, *.gotmpl | |
| jsp | *.jsp | ||
| freemarker | *.ftl |
[#-- the comment --] <ul class="people_list"> [#list items as item] <li>${item}</li> [#/list] </ul> |
|
| aspx | *.aspx, *.ascx | ||
| asp | *.asp | ||
| razor | play, play2 | *.cshtml, *.vbhtml | |
| ctemplate | mustache, handlebars, ember, hapax, ngtemplate | *.hbs, *.mustache |
{{! comment }} <ul class="people_list"> {{#each people}} <li>{{this}}</li> {{/each}} </ul> |
(add-hook 'local-write-file-hooks
(lambda ()
(delete-trailing-whitespace)
nil))
M-x byte-compile-file
(add-to-list 'auto-mode-alist '("\\.php\\'" . php-mode))
(add-to-list 'auto-mode-alist '("\\.blade\\.php\\'" . web-mode))
The matching is done on the path. You can alo configure like this if your templates are stored in a subdirectory called views, html or templates.
(add-to-list 'auto-mode-alist '("/\\(views\\|html\\|templates\\)/.*\\.php\\'" . web-mode))
(local-set-key (kbd "RET") 'newline-and-indent)
You can also use M-x electric-indent-mode
- < fxbois AT Google Mail Service >
Co-founder and CTO of KerniX, Digital Agency (Web/Mobile) in Paris (FR).