Mercurial > cgi-bin > hgweb.cgi > tincan
diff doc/api_reference.rst @ 62:fd8c558a89bb draft
Finished, but not finished proofreading.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 09 Jun 2019 14:16:17 -0700 |
parents | 55828c01e38f |
children | 8867b3a5c4fa |
line wrap: on
line diff
--- a/doc/api_reference.rst Sun Jun 09 10:37:45 2019 -0700 +++ b/doc/api_reference.rst Sun Jun 09 14:16:17 2019 -0700 @@ -38,7 +38,7 @@ .. method:: forward(target) -Perform a programmatic, request-time, server-side redirect to the route specified by *target*, which may be either relative to the route issuing the redirect or an absolute route. This differs from the ``#forward`` header directive in that this method causes a server-side redirect to be set up at route creation time. The ``#forward`` directive is both more efficient and less flexible than the ``forward`` method. +Perform a programmatic, request-time, server-side redirect to the route specified by *target*, which may be either relative to the route issuing the redirect or an absolute route. This differs from the ``#forward`` header directive in that this method causes a server-side redirect to be set up the time a request is processed, not the when a route is created. That makes this method both more flexible and less efficient than ``#forward``. One may only forward from a normal page to another normal page; neither the source nor the target of a forward may be an error page. Attempts to create forward loops will also cause an error. @@ -63,26 +63,50 @@ .. method:: export() -This exports all non-hidden instance variables; it does not export attributes that define callable objects (e.g. methods). A "hidden" instance variable means any one whose name *does not* start with an underscore; the ``request`` and ``response`` instance variables of type ``tincan.Page`` are also considered hidden. Finally, this object itself is exported as the ``page`` variable. This method returns a dict containing the exported items. +This is called by TinCan to export template variables; it must return a ``dict`` or ``dict``-like object. + +The default implementation exports all non-hidden instance variables; moreover, it does not export attributes that define callable objects (e.g. methods). A "hidden" instance variable means any one whose name *does not* start with an underscore; the ``request`` and ``response`` instance variables of class ``tincan.Page`` are also considered hidden. Finally, ``self`` is exported as the ``page`` variable. Note that the exporting happens *after* header processing; thus, if there is a conflict between a template variable defined by the ``#load`` header directive and this exporting logic, the value exported by this method will always overwrite the earlier one. If the above exporting behavior is for some reason unsuitable, it is permitted to override this method. -.. class tincan.Page +.. class:: tincan.Page -The parent class of all normal (non-error) pages. This is a subclass of ``tincan.BasePage`` above. +The parent class of all normal (non-error) pages. -.. class tincan.ErrorPage +.. class:: tincan.ErrorPage The parent class of all error pages. +========== +Exceptions +========== + +.. class:: tincan.TinCanException + +The parent class of all exceptions TinCan raises. + +.. class:: tincan.TemplateHeaderError + +Raised upon encountering a syntax error in the template header directives. + +.. class:: tincan.LoadError + +Raised when we run into problems ``#load``'ing something, usually because it doesn't exist. + +.. class:: tincan.TinCanError + +General-purpose exception raised by TinCan when things go wrong, either when attempting to launch webapps, or attempting to service requests. Often caused by errors in user-created files. + ============================ Request and Response Objects ============================ The ``request`` and ``response`` instance variables of ``tincan.Page`` are standard ``bottle.Request`` and ``bottle.Response`` objects. In the ``environ`` attribute of the ``request`` object, any key beginning with ``tincan.`` is reserved. +.. _header-directives: + ================= Header Directives ================= @@ -102,7 +126,7 @@ This is a hidden page; do not create a route for it. The page can only be displayed by a server-side forward. ``#load`` - Load the specified Chameleon template file and make the loaded template available as a template variable. Useful for importing and invoking macros. See the :ref:`loading-templates` below for more information. + Load the specified Chameleon template file and make the loaded template available as a template variable. Useful for importing and invoking macros. See :ref:`loading-templates` below for more information. ``#methods`` A list of HTTP request methods, separated by whitespace, follows. The route will allow all specified methods. Not specifying this line is equivalent to specifying ``#methods GET``. @@ -111,7 +135,7 @@ What follows is the name of the Python file containing the code-behind for this route; the file name must end in ``.py``. ``#template`` - Ignore the body of this file and instead use the template in the body of the specified file, which must end in .pspx. Any headers in the referred template file are ignored. + Ignore the body of this file and instead use the template in the body of the specified file, which must end in ``.pspx``. Any headers in the referred template file are ignored. Error Pages ----------- @@ -132,14 +156,14 @@ The ``#load`` directive may be used to load additional templates, e.g. ones containing macro definitions. Note that the loaded files are standard Chameleon templates and *not* TinCan ``.pspx`` files (i.e. they cannot contain any header directives); as such, loaded files must have the standard ``.pt`` extension for Chameleon template files. -In the normal case, ``#load foo.pt`` will load a file relative to the same directory as the page containing the ``#load`` directive itself. The loaded template object will be made available as a template variable matching the file name sans extension (e.g. ``foo``). One can change the name of the variable created by prefixing the file specification with a variable name followed by an equals sign, e.g. ``#load t=foo.pt``. If one places the specification inside angle brackets (e.g. ``#load <t=foo.pt>``), loaded files are searched for in ``WEB-INF/tlib`` instead. +In the normal case, ``#load foo.pt`` will load a file relative to the same directory as the page containing the ``#load`` directive itself. The loaded template object will be made available as a template variable matching the file name sans extension (e.g. ``foo``). One can change the name of the variable created by prefixing the file specification with a variable name followed by an equals sign, e.g. ``#load t=foo.pt``. If one places the specification inside angle brackets (e.g. ``#load <t=foo.pt>``), files to be loaded are searched for in ``WEB-INF/tlib`` instead. Finally, as is allowed for all arguments to header directives, one may enclose the argument to ``#load`` inside single or double quotes and use the normal Python string syntax. Using Loaded Macros ------------------- -Once a template has been loaded, it will be available as a sub-attribute of the macros attribute of the associated template object. E.g.:: +Once a template has been loaded, it will be available as a sub-attribute of the ``macros`` attribute of the associated template object. E.g.:: #load foo.pt <!DOCTYPE html>