1.1 --- a/docs/encodings.html Wed Feb 28 19:49:11 2007 +0000
1.2 +++ b/docs/encodings.html Wed Feb 28 22:31:10 2007 +0000
1.3 @@ -43,8 +43,7 @@
1.4 for Unicode objects which let you think of letters, numbers, symbols
1.5 and all other characters in an abstract way.</p>
1.6 <ul>
1.7 - <li>Convert textual content to Unicode as soon as possible (see below
1.8 -for choosing encodings).</li>
1.9 + <li>Convert textual content to Unicode as soon as possible.</li>
1.10 <li>If you must include hard-coded messages in your application code,
1.11 make sure to specify the encoding using the <a href="http://www.python.org/peps/pep-0263.html">standard declaration</a>
1.12 at the top of your source file.</li>
1.13 @@ -68,10 +67,13 @@
1.14 best way to do this is to decide which encoding is most suitable for
1.15 the data
1.16 presented and received in your application and then to use it
1.17 -throughout.
1.18 -Here is an outline of code which does this:</p>
1.19 +throughout.</p><p>One
1.20 +approach which works acceptably for smaller applications is to define
1.21 +an attribute (or a global) which is conveniently accessible and which
1.22 +can be used directly with various transaction methods. Here is an
1.23 +outline of code which does this:</p>
1.24 <pre>from WebStack.Generic import ContentType<br /><br />class MyResource:<br /><br /> encoding = "utf-8" # We decide on "utf-8" as our chosen<br /> # encoding.<br /> def respond(self, trans):<br /> [Do various things.]<br /><br /> fields = trans.get_fields_from_body(encoding=self.encoding) # Explicitly use the encoding.<br /><br /> [Do other things with the Unicode values from the fields.]<br /><br /> trans.set_content_type(ContentType("text/html", self.encoding)) # The output Web page uses the encoding.<br /><br /> [Produce the response, making sure that self.encoding is used to convert Unicode to raw strings.]</pre>
1.25 -<h3>Tell Encodings to Other Components</h3>
1.26 +<h3>Use EncodingSelector to Set the Default Encoding</h3><p>An arguably better approach is to use selectors (as described in <a href="selectors.html">"Selectors - Components for Dispatching to Resources"</a>), typically in a "site map" arrangement (as described in <a href="deploying.html">"Deploying a WebStack Application"</a>), specifically using the <code>EncodingSelector</code>:</p><pre>from WebStack.Generic import ContentType<br /><br />class MyResource:<br /><br /> def respond(self, trans):<br /> [Do various things.]<br /><br /> fields = trans.get_fields_from_body() # Encoding set by EncodingSelector.<br /><br /> [Do other things with the Unicode values from the fields.]<br /><br /> trans.set_content_type(ContentType("text/html")) # The output Web page uses the default encoding.<br /><br /> [Produce the response, making sure that self.encoding is used to convert Unicode to raw strings.]<br /><br />def get_site_map():<br /><br /> return EncodingSelector(MyResource(), "utf-8")</pre><h3>Tell Encodings to Other Components</h3>
1.27 <p>When using other components to generate content (see <a href="integrating.html">"Integrating with Other Systems"</a>), it may
1.28 be the case that such components will just write the generated content
1.29 straight to a normal stream (rather than one wrapped by a <code>codecs</code>