From 12f3796c273ae5e0486d4168949f2ffd1767ba39 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Wed, 2 Mar 2011 15:33:15 +0100 Subject: Permit @ as a variable as well as a section specifier. --- json-template.lisp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'json-template.lisp') diff --git a/json-template.lisp b/json-template.lisp index 237d627..59f2aed 100644 --- a/json-template.lisp +++ b/json-template.lisp @@ -124,6 +124,11 @@ (with-output-to-string (out) (expand-template-to-stream template context out))) +(defun getcontext (context key) + (if (string= key "@") + context + (getf context (intern (string-upcase (string key)) '#:keyword) nil))) + (defun expand-template-to-stream (template context stream) (dolist (thing template) (ecase (first thing) @@ -131,10 +136,7 @@ (write-string (second thing) stream)) (:variable (destructuring-bind (variable filter) (cdr thing) - (let ((value (getf context - (intern (string-upcase variable) - '#:keyword) - nil))) + (let ((value (getcontext context variable))) (format stream "~A" (if filter (funcall (cdr (assoc filter *template-filters*)) @@ -142,16 +144,13 @@ value))))) (:section (destructuring-bind (section branch alternative) (cdr thing) - (let ((value (getf context (intern (string-upcase section) '#:keyword) nil))) + (let ((value (getcontext context section))) (expand-template-to-stream (if value branch alternative) value stream)))) (:repeated-section (destructuring-bind (section branch alternative) (cdr thing) - (let ((value (if (string= section "@") - context - (getf context (intern (string-upcase section) '#:keyword) - nil)))) + (let ((value (getcontext context section))) (if value (mapc (lambda (ctx) (expand-template-to-stream branch ctx stream)) -- cgit v1.2.3