0

I want to use jinja2-mode for files ending ".html" when the variable my:use-jinja-for-html-p is non-nil.

I have the following files:

.dir-locals.el

((nil . ((my:use-jinja-for-html-p . t))))

blah.html

<html>

.emacs

(defvar my:use-jinja-for-html-p nil
  "Use `jinja2-mode' if non-nil, otherwise `html-mode'.
Primarily for use in .dir-locals.el")

(defun my:maybe-choose-jinja2-mode ()
  (when my:use-jinja-for-html-p
    (jinja2-mode)))

(add-hook 'html-mode-hook 'my:maybe-choose-jinja2-mode)

my:maybe-choose-jinja2-mode runs after html-mode, but uses the global value of my:use-jinja-for-html-p instead of the file-local value specified in .dir-locals.el.

Why doesn't my:maybe-chose-jinja2-mode use the file-local value?

Answer for Completeness - see dupe for detail

;; See http://stackoverflow.com/questions/5147060/ for explanation of
;; why we're creating a new hook.  tl;dr: wierd interaction between
;; `set-auto-mode' and `hack-local-variables'
(add-hook 'hack-local-variables-hook 'my:run-local-vars-mode-hook)
(defun my:run-local-vars-mode-hook ()
  "Run a hook for major-mode after processing local variables.
e.g. `python-mode-local-vars-hook',`emacs-lisp-mode-local-vars-hook'"
  (run-hooks (intern (format "%s-local-vars-hook" (symbol-name major-mode)))))

(add-hook 'html-mode-local-vars-hook 'my:maybe-choose-jinja2-mode)
Joe
  • 3,370
  • 4
  • 33
  • 56
  • What is `use-jinja-p`? Did you mean to use `my:use-jinja-for-html-p`? – Drew Jul 26 '14 at 04:05
  • Yep, I simplified the code, but forgot to change that back. – Joe Jul 26 '14 at 04:47
  • And is your HTML file in the proper directory for the `.dir-locals.el` file to control it? – Drew Jul 26 '14 at 05:54
  • The files are in the same directory. If I run M-x eval-expression file-local-variables-alist in blah.html, I see that my:use-jinja-for-html-p is set to t – Joe Jul 26 '14 at 06:53

0 Answers0