A template model provides properties and methods which can be accessed
in the corresponding template script.
The model can be implemented in Java or - since Magnolia 5.5.6 it can
also be written in JavaScript following
Light development
approach - thus enabling fast development and deployment without the
need for Java, Maven or WAR deployment.
It is not necessary to use a model in Magnolia templating, but it is
useful. If you are starting out with Magnolia use
templating
functions instead of a model. Model classes cannot be added to
light modules because light modules cannot
contain their own Java classes. However, a light module can reference a
component
model from another module such as MTE.
A model can execute any Java code to retrieve data from the JCR
repository or from an external source.
What is a model?
A model is written as a Java model class. It is a JavaBean where
business logic resides. The model class is the entry point for custom
Java logic for the template script. All
templates -
page,
area and
component -
can define a model class.
How does a model work?
During the
rendering
process, the renderer calls the model’s execute() before it calls the
template script. The return value of the execute method is passed to
the template script with the actionResult object.
The model class:
Is instantiated for each rendering process.
Is provided to the respective script as an object
model.
Model classes provide their own methods. You can use any public method
in your templates. The methods are exposed using the
model.<method name> notation.
All getters without parameters can be accessed without the
`get' part of the method name and without the method brackets. Methods
that expect parameters can only be called by the full method
declaration.
Example: How to use model methods in your templates