Trivial-String-Template

A Pretty Handy Utility Library

Posted by David Gu on April 16, 2016

Trivial-String-Template

Usage

substitute

(substitute "$who likes $what" :who "tim" :what "kung pao")
;; => "tim likes kung pao"

You can specify the delimiter character by lexcially binding the special variable \*delimiter\*:

(let ((*delimiter* #\%))
  (substitute "%who likes %what" :who "tim" :what "kung pao"))
;; => "tim likes kung pao"

You can escape a delimiter character:

(substitute "Give $who $$100" :who "tim")
;; => "Give tim $100"

And you can use ‘{}’ to set a so called placeholder:

(substitute "$who likes ${what}--is it for real?" :who "tim" :what "kung pao")
;; => "tim likes kung pao--is it for real?"

safe-substitute

(safe-substitute "$who likes $what" :what "kung pao")
;; => "$who likes kung pao"

template

It will produce a template class, which is funcallable. Some accessors methods are exported, by which you can modify the state of a template and don’t you worry about any confilcts because every accessor will automatically adjust and then make itself consistent.

(defvar *tmpl* (template "$who likes $what"))
(funcall *tmpl* :who "a" :what "b")
;; => "a likes b"
(setf (source-string *tmpl*) "Give $who $$100")
(funcall *tmpl* :who "me")
;; => "Give me $100"

define-template

Just a wrapper around template class.

(define-template tmpl (:delimiter #\& :safe t) "&who likes &what")
;; => #<TRIVIAL-STRING-TEMPLATE:TEMPLATE {1003B1FC7B}>
(tmpl :who tim) ;; => "tim likes &what"

TODO

  • Provide a more detailed APIs documenatation.
  • A comprehensive test.

Author

Copyright (c) 2016 David Gu ([email protected])

License

Licensed under the MIT License.