[racket] web-server/templates: "require: not at module level or top level"

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Tue Nov 26 09:59:50 EST 2013

There is very little to say more than what the error message already says.

The documentation for include-template says: "Compiles the template at
path-spec using the @ Syntax syntax within the enclosing lexical
context."

It does not say "Requires the module specified by path-spec". In other
words, include-template is source-inclusion, not module-requiring.
This means that the contents of the template is not a module.

Furthermore, the contents of the template is really one giant
expression that looks like: (with-output-to-string (lambda () (output
(list ...what-you-wrote...))))

So, even if the template were a module, you still couldn't use
require, because it won't be at the top-level without some wrangling.

Of course, local-require works fine because local-require can go at
any scope and position.

Jay





On Mon, Nov 25, 2013 at 10:17 PM, Greg Hendershott
<greghendershott at gmail.com> wrote:
> Why doesn't `require` work in a file provided to `include-template`
> from web-server/templates?
>
> I dug into the source to see how far I could figure this out on my
> own. I see that `include-template` is a thin wrapper around
> `include/text` from scribble/text. I see in syntax-utils.rkt that
> `include/text` is using `include-at/relative-to/reader` with a
> Scribble syntax reader. But at that point it gets over my head.
>
> Motivating example: In their HTML template someone would like to
> `require` a module to do some fancy formatting of a template variable.
> Let's say they get a date, and want to `(require racket/date)` for
> `date->string` and its various options.  Or, they want to do whatever
> else, and it requires (ha) some module.
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93

Posted on the users mailing list.