8.1 The SimpleContext Execution Context

The SimpleContext class is provided for applications which only make use of the Albatross template functionality. If you look at the implementation of the class you will note that it is constructed from a number of mixin classes. Each of these classes implements some of the functionality required for interpreting Albatross templates.

Diagrammatically the SimpleContext class looks like this:

Figure: The SimpleContext class
 
\includegraphics{simplecontext}

By implementing the execution context semantics in a collection of mixin classes Albatross allows you to change semantics by substituting mixins which implement the same interface. This is very useful when using the Albatross application objects.

NamespaceMixin This mixin provides a local namespace for evaluating the expressions embedded in the expr tag attributes. Application code places values into the locals member to make them available for display by the template files.

You will probably always use this mixin in your execution context.

ExecuteMixin This mixin provides a sort of virtual machine which is required by the template file interpreter. It maintains a macro argument stack for expanding macros, and it is used to accumulate HTML produced by template execution.

You will probably always use this mixin in your execution context.

ResourceMixin This mixin provides a registry of template resources which only need to be defined once. Specifically the class provides a dictionary of Python classes which implement template file tags, a dictionary of template macros, and a dictionary of template lookup tables.

If you are using Albatross application functionality, you will almost certainly use this mixin in your application class, not the execution context.

TemplateLoaderMixin This mixin is a very simple template file loader. You will almost certainly use the CachingTemplateLoaderMixin in your application object instead of this mixin when you use the Albatross application objects.

StubRecorderMixin Albatross provides special versions of the standard HTML <input>, <select>, and <form> tags. As these tags are converted to HTML they report back to the execution context. Applications which do not need to record the contents of each form can use this mixin to ignore the form record.

StubSessionMixin

Albatross provides an application session. Applications which do not a session can use this mixin to disable session functionality.

Collectively these classes provide all of the functionality which is required to execute Albatross templates. The following table contains a list of all methods defined in the context.

Method  Mixin 
add_session_vars(*names) StubSessionMixin
clear_locals() NamespaceMixin
del_session_vars(*names) StubSessionMixin
encode_session() StubSessionMixin
eval_expr(expr) NamespaceMixin
flush_content() ExecuteMixin
flush_html() ExecuteMixin
form_close() StubRecorderMixin
form_open() StubRecorderMixin
get_lookup(name) ResourceMixin
get_macro(name) ResourceMixin
get_macro_arg(name) ExecuteMixin
get_tagclass(name) ResourceMixin
get_value(name) NamespaceMixin
has_value(name) NamespaceMixin
has_values(*names) NamespaceMixin
input_add(itype, name, value, return_list) StubRecorderMixin
load_session() StubSessionMixin
load_template(name) TemplateLoaderMixin
load_template_once(name) TemplateLoaderMixin
make_alias(name) NamespaceMixin
merge_request() StubRecorderMixin
pop_content_trap() ExecuteMixin
pop_macro_args() ExecuteMixin
push_content_trap() ExecuteMixin
push_macro_args(args) ExecuteMixin
register_lookup(name, lookup) ResourceMixin
register_macro(name, macro) ResourceMixin
register_tagclasses(*tags) ResourceMixin
remove_session() StubSessionMixin
reset_content() ExecuteMixin
save_session() StubSessionMixin
send_content(data) ExecuteMixin
set_globals(dict) NamespaceMixin
set_save_session(flag) StubSessionMixin
set_value(name, value) NamespaceMixin
should_save_session() StubSessionMixin
write_content(data) ExecuteMixin

Looking inside the context module you will notice some mixin classes which provide alternatives for some of the context functionality. The CachingTemplateLoaderMixin class can be used to replace the TemplateLoaderMixin. Likewise the NameRecorderMixin class is a drop-in replacement for the StubRecorderMixin class. These alternatives are used by some of the prepackaged application objects.

Although all of the template file examples in the Templates User Guide used the SimpleContext class as the execution context, you are much more likely to use something derived from the AppContext class defined in the app module. Since Albatross creates a new execution context to process each browser request, it makes sense to manage tag classes, macros, and lookup tables somewhere other than in the execution context.