<div dir="ltr">BTW here's the technique I devised, in case it's useful to others.<div><br></div><div style>; define the values you need for include-template</div><div style>(define body-for-template "content")</div>
<div style>(define template-name "template.html")</div><div style><br></div><div><div>(define page-result</div><div> (parameterize ([current-namespace (make-base-empty-namespace)]</div><div> [current-directory source-dir] ; set this to your template directory if you want to use relative names in include-template</div>
<div> [current-output-port (open-output-nowhere)])</div><div> (namespace-require 'racket) </div><div> (eval '(require web-server/templates) (current-namespace)) ; repeat as needed for other modules your template uses</div>
<div> (eval `(define body ',body-for-template) (current-namespace)) ; repeat as needed to establish your template variables</div><div> (eval `(include-template #:command-char #\∂ ,template-name) (current-namespace)))) ; I use ∂ as exp delimiter<br>
</div><div><br></div><div><br></div></div><div style>; go on to do things with page-result ...</div><div class="gmail_extra"><br><br>BTW thanks again to Jay McCarthy for adding the command-char option to include-templates, which makes it possible to have compile-time expressions and run-time expressions in the templates, and also avoid escaping @ symbols.<br>
<br><br><div class="gmail_quote">On Wed, May 15, 2013 at 8:43 AM, Matthew Butterick <span dir="ltr"><<a href="mailto:mb.list.acct@gmail.com" target="_blank">mb.list.acct@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im"><div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Probably more products should have an FHQ (Frequently Hopeless Questions) in addition to an FAQ.</span><div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px">I'm making a web-page generator that uses include-template from web-server/templates. I need to set the template inputs & the template itself at runtime, and collect the output.</div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">I tried doing it with module and module* but I didn't see how to make the data go round-trip. A submodule with include-template could require the needed data from the enclosing module, but then the enclosing module can't require the submodule to get the result, so ... ? I could well be overlooking an obvious technique.</div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">My original solution was to write a new file and send it through the Racket executable as a system command. (This worked, slowly.) </div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">So that's how I ended up using eval. Which does work, now that I've retooled how I set up the namespace.</div>
</div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div class="im">On Tue, May 14, 2013 at 5:13 PM, Robby Findler <span dir="ltr"><<a href="mailto:robby@eecs.northwestern.edu" target="_blank">robby@eecs.northwestern.edu</a>></span> wrote:<br>
</div><div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">The short answer is that the top-level is hopeless, as Matthew has discussed at some length here and elsewhere. <div>
<br></div><div>If you really need to eval code, can you first put it into a module?</div>
<div><br>Robby</div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>On Tue, May 14, 2013 at 11:09 AM, Matthew Butterick <span dir="ltr"><<a href="mailto:mb.list.acct@gmail.com" target="_blank">mb.list.acct@gmail.com</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div>Suppose x-is-foo.rkt is this:</div><div><br></div><div><div>(module x-is-foo racket</div>
<div> (define x 'foo)</div>
<div> (provide x))</div><div><br></div><div>If you open another file and try this:</div>
<div><div><br></div><div>(require "x-is-foo.rkt")</div><div>(define x 'bar)</div><div><br></div><div>You'lll get a "identifier already imported" error. OK, that much I understand.</div>
<div><br></div><div>Here's the question. When you do this:</div><div><br></div><div><div>(parameterize ([current-namespace (make-base-empty-namespace)])</div><div> (namespace-require 'racket)</div>
<div> (namespace-require "x-is-foo.rkt")</div><div> (namespace-set-variable-value! 'x 'bar) </div><div> (eval '(print x) (current-namespace)))</div><div><br></div><div>This time, you get 'foo. Why 'foo? Why not another "identifier already imported" error?</div>
<div><br></div><div>I assume I'm missing a subtlety of how the namespace environment is different. But according to the docs, both namespace-require and namespace-set-variable-value! affect the top-level environment of the namespace. So I don't see why the require is silently overriding the set-variable-value, rather than causing a conflict.</div>
<div><br></div><div>It's not a sequencing issue, because if you swap the two lines:</div><div><br></div><div><div>(parameterize ([current-namespace (make-base-empty-namespace)])</div><div> (namespace-require 'racket)</div>
<div> (namespace-set-variable-value! 'x 'bar) </div><div> (namespace-require "x-is-foo.rkt")</div><div> (eval '(print x) (current-namespace)))</div><div><br></div><div>You still get 'foo.</div>
</div><div><br></div><div>Only if you remove the require line:</div><div><br></div><div><div>(parameterize ([current-namespace (make-base-empty-namespace)])</div><div> (namespace-require 'racket)</div>
<div> (namespace-set-variable-value! 'x 'bar) <br></div><div> (eval '(print x) (current-namespace)))</div><div><br></div><div>Do you get 'bar.</div><div><br></div><div><br></div>
</div></div></div></div></div>
<br></div></div>____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></blockquote></div><br></div>
</blockquote></div></div></div><br></div>
</blockquote></div><br></div></div>