Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added programmatic-insert function

Signed-off-by: yonkeltron <yonkeltron@gmail.com>
  • Loading branch information...
commit 222cc1f0669e45bb518fcfa98f8f24632858e82b 1 parent 905419e
@yonkeltron authored
Showing with 18 additions and 0 deletions.
  1. +8 −0 README → README.md
  2. +10 −0 terrible-template.el
View
8 README → README.md
@@ -35,6 +35,14 @@ terrible-template-insert <RET> my-template-name` which will prompt you
for the variable values, interpolate the template and insert it at
point.
+Non-interactive example
+-----------------------
+
+You can still leverage terrible-template non-interactively with the
+`terrible-template-programmatic-insert` function like so:
+
+ (terrible-template-programmatic-insert "my-template-name" '("key1" "val1") '("key1" "val1"))
+
Credits
=======
View
10 terrible-template.el
@@ -87,11 +87,21 @@
template-string))
(defun terrible-template-apply-template (template-string &rest key-value-pairs)
+ "Apply a terrible template by interpolating all variables according to provided values"
(terrible-template-log (concat "Key value pairs: " (prin1-to-string key-value-pairs t)))
(reduce 'terrible-template-substitute
(car key-value-pairs)
:initial-value (copy-sequence template-string)))
+(defun terrible-template-programmatic-insert (template-name &rest key-value-pairs)
+ "Programmatically apply a terrible template"
+ (let ((template-string (plist-get (gethash template-name *terrible-template-global-template-list*) :template-string))
+ (template-variables (plist-get (gethash template-name *terrible-template-global-template-list*) :template-variables)))
+ (if (not (equal (length key-value-pairs)
+ (length template-variables)))
+ (terrible-template-error "Wrong number of key-value pairs provided for template")
+ (terrible-template-apply-template template-string key-value-pairs))))
+
(defun terrible-template-insert (template-name)
(interactive
(list (completing-read "Template name: " (terrible-template-get-template-names))))
Please sign in to comment.
Something went wrong with that request. Please try again.