diff --git a/collects/web-server/scribblings/faq.scrbl b/collects/web-server/scribblings/faq.scrbl index 9fa4d08de0..eaebff32dd 100644 --- a/collects/web-server/scribblings/faq.scrbl +++ b/collects/web-server/scribblings/faq.scrbl @@ -1,11 +1,12 @@ #lang scribble/doc @(require "web-server.ss") -@(require (for-label web-server/dispatchers/dispatch-servlets)) @title[#:tag "faq"]{Troubleshooting and Tips} @section{Why are my servlets not updating on the server when I change the code on disk?} +@(require (for-label web-server/dispatchers/dispatch-servlets)) + By default, the server uses @scheme[make-cached-url->servlet] to load servlets from the disk. As it loads them, they are cached and the disk is not referred to for future requests. This ensures that there is a single namespace for each servlet, so that different instances @@ -41,3 +42,47 @@ In quirks mode, IE does not parse your page as XML, in particular it will not re "empty tag shorthand", e.g. "", whereas the @web-server uses @schememodname[xml] to format XML, which uses empty tag shorthand by default. You can change the default with the @scheme[empty-tag-shorthand] parameter: @scheme[(empty-tag-shorthand 'never)]. + +@section{How do I use templates ``dynamically"?} + +@(require (for-label web-server/templates)) + +A common feature request is to include one template in another dynamically. It should hopefully be obvious that @scheme[include-template] can be included in a template to include a @emph{static} sub-template. For example, +@schemeblock[ + (include-template "posts.html") +] +may appear inside the @filepath{blog.html} template. But you will quickly find that @scheme[(include-template _expr)] will fail when @scheme[_expr] is not syntactically a path, e.g.: +@schemeblock[ +.... +(include-template (if logged-in? + "user-info.html" + "auth.html")) +.... +] + +What is the solution? The templating system already allows you to parameterize templates so particular components come from the including scope. There is no reason those values can not be the results of other templates. In the previous example, suppose the includer was +@schemeblock[ +(define (main-page logged-in?) + (include-template "site.html")) +] + +We could change it to: +@schemeblock[ +(define (main-page logged-in?) + (define user-content + (if logged-in? + (include-template "user-info.html") + (include-template "auth.html"))) + (include-template "site.html")) +] + +and @filepath{site.html} to: +@schemeblock[ +.... +user-content +.... +] + +This allows you to do the same thing but is safer and more efficient: safer because there is no way to include templates that are not named by the programmer and more efficient because all the templates are compiled (and optimized) with the rest of the code. + +If you insist on dynamicism, there is always @scheme[eval]. \ No newline at end of file diff --git a/collects/web-server/scribblings/templates.scrbl b/collects/web-server/scribblings/templates.scrbl index 48812a6002..24c21035eb 100644 --- a/collects/web-server/scribblings/templates.scrbl +++ b/collects/web-server/scribblings/templates.scrbl @@ -109,7 +109,7 @@ title line of different calls to @scheme[fast-template]: (fast-template 'Templates) ] @verbatim[#:indent 2]|{ - Fastest Templates in the West! + ...Fastest Templates in the West!... }| } @@ -118,7 +118,7 @@ title line of different calls to @scheme[fast-template]: (fast-template 42) ] @verbatim[#:indent 2]|{ - Fastest 42 in the West! + ...Fastest 42 in the West!... }| } @@ -127,7 +127,7 @@ title line of different calls to @scheme[fast-template]: (fast-template (list "Noo" "dles")) ] @verbatim[#:indent 2]|{ - Fastest Noodles in the West! + ...Fastest Noodles in the West!... }| } @@ -136,7 +136,7 @@ title line of different calls to @scheme[fast-template]: (fast-template (lambda () "Thunks")) ] @verbatim[#:indent 2]|{ - Fastest Thunks in the West! + ...Fastest Thunks in the West!... }| } @@ -145,7 +145,16 @@ title line of different calls to @scheme[fast-template]: (fast-template (delay "Laziness")) ] @verbatim[#:indent 2]|{ - Fastest Laziness in the West! + ...Fastest Laziness in the West!... +}| +} + +@item{ +@schemeblock[ + (fast-template (fast-template "Embedding")) +] +@verbatim[#:indent 2]|{ + ...Fastest ...<title>Fastest Embedding in the West!... in the West!... }| } ]