Jython wrapper - FreeMarker Manual

- Home
- Manual
- Java API

- Contribute
- Report a Bug
- Download

Manual
Previous Next

# Jython wrapper

The `freemarker.ext.jython` package consists of models that enable any Jython object to be used as a `TemplateModel`. In the very basic case, you only need to call the

    public TemplateModel wrap(Object obj);

method of the `freemarker.ext.jython.JythonWrapper` class. This method will wrap the passed object into an appropriate `TemplateModel`. Below is a summary of the properties of returned model wrappers. Let's assume that the model that resulted from the `JythonWrapper` call on object `obj` is named `model` in the template model root for the sake of the following discussion.

## TemplateHashModel functionality

`PyDictionary` and `PyStringMap` will be wrapped into a hash model. Key lookups are mapped to the `__finditem__` method; if an item is not found, a model for `None` is returned.

## TemplateScalarModel functionality

Every python object will implement `TemplateScalarModel` whose `getAsString()` method simply delegates to `toString()`.

## TemplateBooleanModel functionality

Every python object will implement `TemplateBooleanModel` whose `getAsBoolean()` method simply delegates to `__nonzero__()` in accordance with Python semantics of true/false.

## TemplateNumberModel functionality

Model wrappers for `PyInteger`, `PyLong`, and `PyFloat` objects implement `TemplateNumberModel` whose `getAsNumber()` method returns `__tojava__(java.lang.Number.class)`.

## TemplateSequenceModel functionality

Model wrappers for all classes that extend `PySequence` will implement `TemplateSequenceModel` and thus their elements will be accessible by index using the `model[i]` syntax, which will delegate to `__finditem__(i)`. You can also query the length of the array or the size of the list using the `model?size` built-in, which will delegate to `__len__()`.

Previous Next