Adding arbitrary content to the html <head> section

18 views
Skip to first unread message

Alan Anderson

unread,
Apr 6, 2011, 4:10:25 PM4/6/11
to wub-dis...@googlegroups.com
[tap tap tap...is this thing on?]
 
I'm enjoying great success using Wub as a foundation for my application, but it's still too opaque for me to figure out how to do certain novel things. My need right now is to put some specific text in the <head> section of a page, specifically a Javascript reference:
 
  <script type="text/javascript" src="template.js"></script>
 
I assume I can do that by adding something to the response dict, but what do I add? I would look at and near the Wub code for creating the html <title>, but I can't find it.

Tom Krehbiel

unread,
Apr 6, 2011, 6:05:57 PM4/6/11
to wub-dis...@googlegroups.com
Try something like this
dict set rsp -head "<script type='text/javascript' src='template.js'></script>"
before you return the response. I've been told that's how it's done but haven't tried it myself.
tomk

Colin McCormack

unread,
Apr 6, 2011, 7:54:05 PM4/6/11
to wub-dis...@googlegroups.com

Ok. Complex and hopefully interesting story ...

I presume you're using Direct domain, or Mason domain. Please advise if
this is not so.

Context: Each of the methods which return a response dict to the server
provide for a content type to be specified (usually) as the last
argument as a mime type. Most domains default to returning a
pseudo-type: x-text/html-fragment. There's a post-processing step
handled by Convert.tcl which attempts to transform content into a mime
type that the client has requested. So, returning an
x-text/html-fragment type will lead to its conversion into text/html (if
that's what the client was expecting.)

x-text/html-fragment applies some response dict elements to the
generated code. So -title will be emitted as <title> and so forth. You
can see, from conversions.tcl proc .x-text/html-fragment.text/html
precisely what extras are added in, if you like.

Html.tcl (which should be present everywhere code's running) provides a
series of helper commands to do the messy manipulation for you, and I
would recommend their use (or their improvement, if they don't do what
you want.) In any case, I think they're the appropriate level of
intervention.

[Html header r text] adds arbitrary text to <head> section of response
[Html prescript r url] will cause $url to be added as a <script> to <head>
[Html script r url] will cause the appropriate <script> to be added at
the *end* of content
[Html postscript r script] will cause the verbatim <script> to be added
at the end of content
[Html style r url] will cause the $url <style> to be added to <head>
[Html prestyle r style] will cause the verbatim <style> to be added to
the end of content

Each of these returns a suitably modified response dict. I see that
there's some conflict and inconsistency in the naming of these, and
would like feedback on how they should be named.

This set of operations generally supports the notion that it's quicker
and better to add <style> to <head>, and <script> to <body> where
possible, as doing it that way means stuff gets rendered quicker.

Anyway, feedback welcome.

Colin.


Tom Krehbiel

unread,
Apr 7, 2011, 12:47:35 AM4/7/11
to wub-dis...@googlegroups.com
Colin,

Did you mean
[Html prestyle r style] will cause the verbatim <style> to be added to the end of header
instead of
[Html prestyle r style] will cause the verbatim <style> to be added to the end of content

My understanding is that the style tag can only be used in the header.

Other than that the commands look good.

tomk

Alan Anderson

unread,
Apr 7, 2011, 8:56:55 AM4/7/11
to Wub Discussion
On Apr 6, 7:54 pm, Colin McCormack <mcc...@gmail.com> wrote:
> I presume you're using Direct domain, or Mason domain.  Please advise if
> this is not so.

Yes, I'm using Direct. Apologies for not saying so at first.

> x-text/html-fragment applies some response dict elements to the
> generated code.  So -title will be emitted as <title> and so forth.  You
> can see, from conversions.tcl proc .x-text/html-fragment.text/html
> precisely what extras are added in, if you like.

proc .x-text/html-fragment.text/html {rsp} {
set content "<html> \n"
append content "<header><title>Wrapped</title></header>" \n
append content <body> \n
append content [dict get $rsp -content]
append content </body> \n
append content </html> \n
return [dict replace $rsp \
-content $content \
-raw 1 \
content-type text/html]
}

It doesn't look like <title> is doing anything useful. I don't see
anyplace for "extras" to get included, either. I'm very confused about
how this proc is supposed to work, especially since I thought the
correct tag was <head> and not <header>.

> [Html prescript r url] will cause $url to be added as a <script> to <head>

So I put this in at the same place I'm setting the page title:

Html prescript r template.js

When I try it, it causes a Server Error:

unknown or ambiguous subcommand "prescript": must be argsplit,
attr, default, dict2json, dict2table, dir2table, links, menulist,
olinks, parseAttr, postscript, script, style, table, template, or
ulinks

I'm probably doing something wrong that's both stupid and obvious, but
I don't see what.

Colin McCormack

unread,
Apr 7, 2011, 10:03:00 AM4/7/11
to wub-dis...@googlegroups.com
On 07/04/11 22:56, Alan Anderson wrote:
> On Apr 6, 7:54 pm, Colin McCormack<mcc...@gmail.com> wrote:
>> I presume you're using Direct domain, or Mason domain. Please advise if
>> this is not so.
> Yes, I'm using Direct. Apologies for not saying so at first.
>
>> x-text/html-fragment applies some response dict elements to the
>> generated code. So -title will be emitted as<title> and so forth. You
>> can see, from conversions.tcl proc .x-text/html-fragment.text/html
>> precisely what extras are added in, if you like.
> proc .x-text/html-fragment.text/html {rsp} {
> set content "<html> \n"
> append content "<header><title>Wrapped</title></header>" \n
> append content<body> \n
> append content [dict get $rsp -content]
> append content</body> \n
> append content</html> \n
> return [dict replace $rsp \
> -content $content \
> -raw 1 \
> content-type text/html]
> }

Where'd that come from? The one I mean is in Wub/conversions.tcl ...


> It doesn't look like<title> is doing anything useful. I don't see
> anyplace for "extras" to get included, either. I'm very confused about
> how this proc is supposed to work, especially since I thought the
> correct tag was<head> and not<header>.
>
>> [Html prescript r url] will cause $url to be added as a<script> to<head>
> So I put this in at the same place I'm setting the page title:
>
> Html prescript r template.js
>
> When I try it, it causes a Server Error:
>
> unknown or ambiguous subcommand "prescript": must be argsplit,
> attr, default, dict2json, dict2table, dir2table, links, menulist,
> olinks, parseAttr, postscript, script, style, table, template, or
> ulinks
>
> I'm probably doing something wrong that's both stupid and obvious, but
> I don't see what.

What version is this?

Colin.

Alan Anderson

unread,
Apr 7, 2011, 10:39:30 AM4/7/11
to wub-dis...@googlegroups.com
On Thursday, April 7, 2011 10:03:00 AM UTC-4, mcccol wrote:

Where'd that come from?  The one I mean is in Wub/conversions.tcl ...

Oops. I found that proc in *Convert.tcl*, right after conversion.tcl in an alphabetical directory listing. I'll ignore it and focus on the one in the proper file.
 
But now I'm unsure about when and why it gets called. If my code already explicitly sets the mimetype to text/html, does that mean that I'm responsible for wrapping everything I want in the <html><head></head><body></body></html> tags myself? Actual html headers like Cache-control appear to be working correctly, but now I see that content like <title> isn't.
 

>    unknown or ambiguous subcommand "prescript": must be argsplit,
> attr, default, dict2json, dict2table, dir2table, links, menulist,
> olinks, parseAttr, postscript, script, style, table, template, or
> ulinks
>
> I'm probably doing something wrong that's both stupid and obvious, but
> I don't see what.

What version is this?

 
Wub-4.0.0 (the one labeled "Lastest Wub + WubTk"). I downloaded it last November just before I started serious work on my application, apparently just a few days before 5.0.0 was posted. I guess I'm behind the times, huh? I'll save off my app code and install the new version. I hope there isn't anything different about the way my custom Application.tcl and site.config files have to be written...?

Colin McCormack

unread,
Apr 7, 2011, 10:48:42 AM4/7/11
to wub-dis...@googlegroups.com
On 08/04/11 00:39, Alan Anderson wrote:
On Thursday, April 7, 2011 10:03:00 AM UTC-4, mcccol wrote:

Where'd that come from?  The one I mean is in Wub/conversions.tcl ...

Oops. I found that proc in *Convert.tcl*, right after conversion.tcl in an alphabetical directory listing. I'll ignore it and focus on the one in the proper file.
 
But now I'm unsure about when and why it gets called.

Now that's a great question!  There's a post-processing step, part of content negotiation, where if the content type in the response is not one which is accepted by the browser, a directed acyclic graph of transformations is consulted (they're processes named .from.to and registered with Convert) and if a sequence of transformations can be applied to convert the supplied type to a desired/accepted type, they are called and the derived/transformed type is returned.

That all happens automagically.


If my code already explicitly sets the mimetype to text/html, does that mean that I'm responsible for wrapping everything I want in the <html><head></head><body></body></html> tags myself? Actual html headers like Cache-control appear to be working correctly, but now I see that content like <title> isn't.

Yeah, I wouldn't do that.  Direct defaults to x-text/html-fragment, and that will do the postprocessing transformation, which is (I think) what you want.

You can assemble it all yourself if you want, but why bother?


 

>    unknown or ambiguous subcommand "prescript": must be argsplit,
> attr, default, dict2json, dict2table, dir2table, links, menulist,
> olinks, parseAttr, postscript, script, style, table, template, or
> ulinks
>
> I'm probably doing something wrong that's both stupid and obvious, but
> I don't see what.

What version is this?

 
Wub-4.0.0 (the one labeled "Lastest Wub + WubTk"). I downloaded it last November just before I started serious work on my application, apparently just a few days before 5.0.0 was posted. I guess I'm behind the times, huh? I'll save off my app code and install the new version. I hope there isn't anything different about the way my custom Application.tcl and site.config files have to be written...?

Well behind the times.  Grab the svn version, that's got fewer and newer bugs.

site.config is much the same, except now you can use /the/path in the name part of each section declaring a domain, and dispense with the url element.  It looks neater, IMHO.

Colin.

Alan Anderson

unread,
Apr 7, 2011, 11:47:45 AM4/7/11
to wub-dis...@googlegroups.com
On Thursday, April 7, 2011 10:48:42 AM UTC-4, mcccol wrote:
If my code already explicitly sets the mimetype to text/html, does that mean that I'm responsible for wrapping everything I want in the <html><head></head><body></body></html> tags myself? Actual html headers like Cache-control appear to be working correctly, but now I see that content like <title> isn't.

Yeah, I wouldn't do that.  Direct defaults to x-text/html-fragment, and that will do the postprocessing transformation, which is (I think) what you want.

You can assemble it all yourself if you want, but why bother?
 
Why? Lack of a good understanding of how much richness and magic is available to me, I suppose. I'm used to tclhttpd's paradigm where the return value from a url-mapped "/proc" is sent to the client pretty much verbatim. I can try to take a few steps back from my obsession with detail and rely on Wub to do the right thing for me.
 
Grab the svn version, that's got fewer and newer bugs.
 
I get the impression that "the svn version" isn't the same as "Wub-5.0.0". My problem is that I haven't found a way to do svn through the firewall I'm behind, and all I can manage to do is download a posted archive file.
Reply all
Reply to author
Forward
0 new messages