On Tue, Mar 29, 2011 at 18:44, Saul Hazledine <shaz...@gmail.com> wrote:
> I had a think about using Clojure rather than go to a separate
> template system. Here's a horrible hack that uses eval to support
> string templates:
>
> (ns clj-template.core
>  (:require [clojure.contrib.string :as string]))
>
> (defn remove-templating [s]
>  (string/replace-re #"#" "\"" s))
>
> (defn build-code [s]
>  (str "(str \"" (remove-templating s) "\")"))
>
> (defmacro foreach [loop-stuff & body]
>  `(apply str
>          (for ~loop-stuff
>            (str ~@body))))
>
> (defn eval-template [s]
>  (-> (build-code s)
>      read-string
>      eval))
>
> In the templates anything surrounded by #'s (hashes) is a clojure form
> that should be a compatible argument to str.
>
> On Mar 29, 3:13 pm, B Smith-Mannschott <bsmith.o...@gmail.com> wrote:
>>
>> (defn constructor
>>  [{:keys [class-name fields] :as cfg}]
>>  ["    " class-name "(" (formal-params cfg) ") {\n"
>>   (statement-list
>>    (for [f fields]
>>      ["        this." f " = " f ]))
>>   "    }\n"])
>>
>
> Then becomes:
>
> (eval-string "
> public Example(#(format-params cfg)#)
> {
>        #(foreach [f fields]#
>        this.#f# = #f#;
>        #)#
> }")

Horrible hack, maybe, but it got me thinking. What you seem to be
doing is moving between "code" and "literal" mode by quoting with #.
This is a little like traditional quasi-quote...

And that got me thinking about Scribble [1] again.  In this context I
think of scribble as being sort of an inverse of normal scheme syntax.
In the end, the scribble reader produces the same kind of data
structures as the normal scheme reader, but the emphasis is moved from
code to textual content. Source is content by default, but you can
escape into logic.

That's what your little hack made me think of.  The text embedding in
the string literal is content by default, but # # allows one to escape
into "code mode".

Perhaps a Scribble's approach could be useful for a templating system?
I'm not sure. I haven't really grokked it yet.

[1] http://download.plt-scheme.org/doc/html/scribble/index.html

// Ben

> The general idea might be applicable to what you want.
>
> Saul
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to