# SimpleTAL API: simpleTALES Module.

Documentation on how to use the simpleTALES Module.

## simpleTALES

The only external class in this module is the Context class.

### Context ([options] [,allowPythonPath])

Creates a new Context object, used by SimpleTAL when expanding a template. The options variable, if passed, will be made available as a global variable under the name "options" as per the TALES specification.

By default Python TAL paths (e.g. 'python: 1 + 2') are not allowed. If you require them, and you completely trust the authors of the templates, they can be enabled by passing in allowPythonPath=1.

#### Context.addGlobal (name, value)

Adds the value to the context under name. Value can either be a fundamental python data type or a callable object. For example, take the code:

    class Test:
    	def __init__ (self, val):
    		self.val = val

    	def getResult (self):
    		return str (self.val + 4)

    test = Test (10)

    context = SimpleTALES.Context()
    context.addGlobal ("number14", test.getResult)
    context.addGlobal ("data", {"one": {"blue": ("Cat", "Dog", "Mouse"), "pink": ["One"]}})

The affect of this context is shown for several different TALES expressions:

    <b tal:define="test1 nocall:number14"></b>

The method 'getResult' is bound to the local variable test1.

    <b tal:define="test2 number14"></b>

The local variable test2 is assigned the value "14"

    <b tal:repeat="pets data/one/blue"><i tal:replace="pets"></i></b>

Prints out <b>CatDogMouse</b>

All strings placed into the context, and all strings returned by callable objects should be unicode strings. This allows the rendering of the template into different character sets without requiring any code changes.

### PathFunctionVariable (callableObject)

This class wraps a callable object (e.g. function) so that it can receive part of a TAL path as it's argument. To use this simply create a new instance of the PathFunctionVariable and then place this into the Context (see above). The path passed to the function is that part of the path not already used. For example if the function "helloFunc" is placed in the Context the path "helloFunc/an/example" results in the string "an/example" being passed to the function.

### CachedFuncResult (callableObject)

This class wraps a callable object (e.g. function) so that the callable is only called once. In normal SimpleTAL operation any function placed into a Context will be called multiple times during template expansion. To ensure that it is only called once simply wrap in the CachedFuncResult object first.

#### clearCache ()

Clears the cache. Use this to clear the cache between multiple template expansions if the callable should be executed once per template expansion.

Back to SimpleTAL API

PubTal Version