RIFE contains limited scripting support for templates. Contrary to how scripting is used in other web application frameworks, RIFE doesn't allow it to retrieve values and fill them into the template since that would make templates more active than we like.
Purpose
Scripting is used to provide boolean expressions that will be evaluated at runtime to automatically assign the content of blocks to values.
For example:
Evaluated to false
Evaluated to true
In this example, the expression in between the [[ and ]] will be evaluated. If the expression returns true, the value will be replaced with 'Evaluated to true'. If the expression retuns false however, the value will be not be replaced by any block but keep its default content: 'Evaluated to false'. This allows for on-the-fly template changes with or without developer intervention.
[top]
Supported languages
Currently we support OGNL
, Groovy
and Janino
. To identify which language to use, you have to use the following blockvalue ID prefixes:
[top]
Expression variables
For the expressions to be useful, they have to be evaluated against a context. Therefore it's possible to set variables from an element that can be easily accessed from within the scripted expression.
For example:
In an element
public class ScriptingTest extends Element
{
public void processElement()
{
Template template = getHtmlTemplate("test.ognl");
template.setExpressionVar("show_block", "yes");
print(template);
}
}
In a template
Block is NOT being displayed
Block is being displayed
Apart from the expression variables, each expression is evaluation against a current root object whose methods and properties you can access using the regular scripting language syntax. This root object is by default the template instance that you are processing.
[top]
Specialized scripted tags
To make it easy to write expressions against commonly used contexts, RIFE also provides specialized scripted tags that set different root objects. Currently the language:ROLEUSER and language:CONFIG tags have been provided.
OGNL:ROLEUSER, GROOVY:ROLEUSER, JANINO:ROLEUSER
The root object will be the RoleUserAttributes
of an identified user and it will be null if no identification could be performed. Since the user attributes don't contain any login property; the login of the user is set as an expression variable. For consistancy all the other user attributes are also provided through variables.
in OGNL
in Groovy
in Janino
This specialized scripted tag makes it very easy to conditionally show parts of an interface according to the credentials of a user.
For example:
User is not in role "admin"
User in named "moderator"
User is in role "admin"
User is named "moderator"
[top]
OGNL:CONFIG, GROOVY:CONFIG, JANINO:CONFIG
The root object will be the default configuration instance: Config.getRepInstance(). This tag can for example be used to create variants of an application and conditionally show parts of the interface according to configuration values. You can thus maintain a single codebase and just modify the configuration for different installations.
For example:
DISPLAY_BLOCK is false<!--/V-->
STRING_VALUE is 'do not match'<!--/V-->
DISPLAY_BLOCK is true
STRING_VALUE is not 'do not match'
[top]
Evaluation order
Scripted blocks are always evaluated late (i.e. at time of template printing). If there is a need to evaluate early or repetitively, you can use the following three methods:
public class OGNLTest extends Element
{
public void processElement()
{
Template template = getHtmlTemplate("test.ognl");
t.evaluateOgnl("ognl_block");
t.evaluateOgnlConfig("ognl_config_block");
evaluateOgnlRoleUser(t, "ognl_role_user_block");
print(t);
}
}
Please note that the method to evaluate an language:ROLEUSER block is not a member of Template, but an inherited method of Element since it needs to access the request context.
[top]