I am using Emacs to write XHTML pages in a JSF project and the amount of boilerplate code that has to go at the header of each XHTML file is hard-to-type, ugly and error-prone. E.g:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
What would be the "emacs way" of dealing with this? I 've read about the nXhtml mode but haven't tried it and don't know if it auto-completes boilerplate like the above or just offers more rudimentary XML checking / completion syntax. At any rate I would like to evaluate more lightweight alternatives first.
search for "template emacs". https://www.google.com/search?q=template+engine+emacs
here's a good survey: http://www.emacswiki.org/emacs/CategoryTemplates .
I use defaultcontent.el, which is one of the options listed on that page. Defaultcontent.el fills new files with a template depending on a pattern match on the filename. All .html files can get a particular template. You can do more advanced mapping of templates to files; defaultcontent.el uses an alist and you can specify any regex for the filename you like.
The template that gets placed into the new file can expand various things dynamically, including the filename, basefilename, date/time, environment variables. In more advanced scenarios the template can run elisp code that you provide.
It's a good companion to yasnippet, which I use for optional pieces of code within a file. For example, my base .htm template (inserted by defaultcontent.el) doesn't include a script reference for jquery. But I have a yasnippet for jquery , so that when I type (jquery[TAB]), I get the full expansion of
<script type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js'>
</script>
defaultcontent.el uses find-file-hooks and inserts template contents only when a file is initially created.
I use it for virtually all file formats.