This is comparison of rcd-template-eval function with eev-template0.el from Eduardo Ochs, as in his the package eev. This is educational page that talks on how to use the RCD Template Interpolation System for Emacs.

This is excerpt from discussion on GNU Emacs Users Mailing List.

just as a curiosity, here is how eev implements template interpolation:

I tried several other implementations of template interpolation functions before that one, and from time to time I would find another way that would be both simpler and more powerful than the previous one…​ the one above is so simple that in one of the times that I gave a minicourse on LaTeX and Emacs I followed the code of ee-template0 with the students in the third day of the minicourse - one day after teaching them briefly how to understand code with `defun`s and `let`s.

— Eduardo Ochs, Author of eev Emacs package

That is so interesting. The rcd-template-eval has slightly different concept. In my eval-ing, I have to get some value no matter what, I am also thinking I have freedom to make errors, to the system is not allowed to fail. Here is demonstration:

(setq rcd-template-delimiter-open "(")  "("
(setq rcd-template-delimiter-close ")")  ")"
(setq text1 "We will evaluate (+ 2 2) to result ((+ 2 2))") ;; won't interpolate
(setq text2 "We will evaluate (+ 2 2) to result ( (+ 2 2) )")
(setq text3 "We will evaluate (+ 2 2) to result ( (+ 2 2 )") ;; error allowed

This does not interpolate, as I demand spaces after delimiters:

(rcd-template-eval text1)  "We will evaluate (+ 2 2) to result ((+ 2 2))"

This does interpolate:

(rcd-template-eval text2)  "We will evaluate (+ 2 2) to result 4"

Error is interpolated to "", but I could log it in future:

(rcd-template-eval text3)  "We will evaluate (+ 2 2) to result "

Compared to this that works:

(ee-template0 "{<} a{(+ 2 3)} {>}")  "{ a5 }"

And this one giving error:

(ee-template0 "{<} a{(+ 2 3} {>}")

Concept is the same. You also do not use dynamic binding, which is quite clear why.

In the sense of teaching is fine. I guess you almost never had a problem when evaluation fails for some reason.

In the sense of generation of hundreds of emails for people, or generation of thousands of HTML pages, the template system IMHO is not allowed to fail. Errors however, could be reported, templating system could then tell me in which page or email the error has been found. But it has to move silently over the failure.

Thus I would expect also this one, to produce following:

(ee-template0 "{<} a{(+ 2 3} {>}")   "{ a }"

And more perfect would be if error would be reported somewhere in silent.

My trick to handle lexical binding was to tell the students that dynamic binding is much easier to understand than lexical binding, and that they should treat dynamic binding as an advanced topic that would make more sense when they had at least one week of experience with Emacs - and my minicourse was only five days long, from monday to friday…​ but the students in that minicourse were mathematicians with little experience in programming, not compsci people who would see immediately that dynamic binding is "wrong".
— Eduardo Ochs, Author of eev Emacs package

Thus you had the luck to have a different type of audience.

Here is also demonstrated how eev tools have different context, programs are invoked from within text manuall. Your tools are in general, beyond templating, meant to be evaluated in the text or within the text and in that concept there is also no need for lexical binding. Small snippets are evaluated one after each other by user’s demand, functions evaluated may come originally from programs with lexical binding.

Thus I have improved my function to evalute with errors reported, as it makes so much more sense. It is made to interpolate thousands of times within minutes automatically.

Tip
Original fresh and updated Source: https://gnu.support/files/sources/rcd-template.el
(defun rcd-template-eval (string &optional delimiters hash)
  "Evaluates Emacs Lisp enclosed by `rcd-template-delimiter-open' and `rcd-template-delimiter-close'.

Optional DELIMITERS list may be provided to change default
delimiters, first list member has to be the opening delimiter and
second the closing delimiter.

HASH may be supplied to interpolate values from hash key . When HASH keys are strings, the hash table :test must be
'equal.

Space or new line has to follow `rcd-template-delimiter-open' and
precede `rcd-template-delimiter-close' for evaluation to get
invoked.

The function will evaluated to "" on any error.

Errors are reported on the standard error output."
  (let* ((delimiters (or delimiters (list rcd-template-delimiter-open rcd-template-delimiter-close)))
	 (open (car delimiters))
	 (close (cadr delimiters))
	 (add (length open))
	 (minus (length close))
	 (keys (if hash (hash-table-keys hash)))
	 (test (when hash (hash-table-test hash)))
	 (debug-on-error nil))
    (with-temp-buffer
      (insert string)
      (goto-char 0)
      (while (re-search-forward (rx (literal open)
				    (one-or-more (or blank "\n"))
				    (group (minimal-match (one-or-more anything)))
				    (one-or-more (or blank "\n"))
				    (literal close))
				nil t)
	(let* ((match-beginning (match-beginning 0))
	       (match-end (match-end 0))
	       (match (buffer-substring-no-properties
		       (+ add match-beginning) (- match-end minus)))
	       (lisp (condition-case error
			 (read-from-string match)
		       (error (princ (format "rcd-template-eval: `read-from-string' error: %s for match: '%s'" error match))
			      "")))
	       (lisp (if (listp lisp) (car lisp) lisp))
	       (match (string-trim (prin1-to-string lisp)))
	       (parenthesis (substring match 0 1))
	       (lisp (car (read-from-string match)))
	       (value (condition-case error
			  (if (and
			       (not (string= parenthesis "("))
			      keys
			    (member
			     (cond ((eql test 'eql) lisp)
				   ((eql test 'equal) (symbol-name lisp))
				   (t lisp))
			     keys))

			      (gethash
			       (cond ((eql test 'eql) lisp)
				     ((eql test 'equal) (symbol-name lisp))
				     (t lisp))
			       hash "")

			    (eval lisp))
			(error (princ (format "rcd-template-eval: `eval' error: %s for match: '%s'" error match))
			       "")))
	       (value (cond ((null value) "")
			    (t (format "%s" value)))))
	  (delete-region match-beginning match-end)
	  (insert (format "%s" value))))
      (buffer-string))))
(rcd-template-eval "( (+ 2 2) )" '("(" ")"))  "4"
(rcd-template-eval "( (+ 2 2 )" '("(" ")"))  ""
     with error reporting in *Messages* buffer:
     rcd-template-eval: `read-from-string' error: (end-of-file)
(rcd-template-eval "We expect 4 but fail: ( (+2 2) )" '("(" ")"))  "We expect 4 but fail: "
     with error reporting in *Messages* buffer:
     rcd-template-eval: `eval' error: (invalid-function 2)
(defun ee-template-test (text)
  (rcd-template-eval text '("{" "}")))

My regexp asks for spaces or new lines between delimiters:

(ee-template-test "{<} a{ (+ 2 3 } {>}")  "{<} a {>}"
     with error reporting in *Messages* buffer:
     rcd-template-eval: `read-from-string' error: (end-of-file)

but of course, it does not do your trick:

(ee-template-test "{<} a{ (+ 2 3) } {>}")  "{<} a5 {>}"

If I would like to enclose it with open delimiters in a result, this would work:

(ee-template-test "{ \"{\" } a{ (+ 2 3) } { \"}\" }")  "{ a5 }"

And I use the habit that people would not tend to write space after parenthesis.

I could write: (maybe I am wrong), but I would never write ( maybe I am not wrong ).

That may allow using parenthesis in templates which makes it more Lisp-like:

(rcd-template-eval "We know that (+ 2 2) evaluates to ( (+ 2 2) )")  "We know that (+ 2 2) evaluates to 4"

Then `eval' within `eval' help in avoiding repetition of demonstrated code:

(rcd-template-eval "We know that ( (setq code '(+ 2 2)) ) evaluates to ( (eval code) )")  "We know that (+ 2 2) evaluates to 4"

Regular expression is I see hard coded:

(defvar ee-template00-re "{\\([^{}]+\\)}")

And I have envisioned it to rather have flexible delimiters:

(rcd-template-eval "<% user-login-name %>" '("<%" "%>"))  "jimmy"

as it emulates some other templating engines and allows re-use of templates:

(rcd-template-eval "<% @var user-login-name %>" '("<% @var" "%>"))  "jimmy"

Maybe you wish to include error reporting and silent failure as well, or no?

Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/
https://rms-support-letter.github.io/

GNU Free Documentation License

Copyright © 2021-05-03 08:52:42.814279+02 by Jean Marc Louis. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License"