Dashboard > RIFE > ... > Templates > Blockvalue scripting support
RIFE Log In | Sign Up   View a printable version of the current page.
Blockvalue scripting support


Added by Geert Bevin, last edited by Geert Bevin on Apr 11, 2005  (view change)
Labels: 
(None)


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:

<!--V 'OGNL:name'-->Evaluated to false<!--/V-->

<!--B 'OGNL:name:[[ true ]]'-->
    Evaluated to true
<!--/B-->

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:

OGNL:
GROOVY:
JANINO:

[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

<!--V 'OGNL:name'-->Block is NOT being displayed<!--/V-->

<!--B 'OGNL:name:[[ #show_block == "yes" ]]'-->
    Block is being displayed
<!--/B-->

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

#login
#password
#userId
#roles

in Groovy

login
password
userId
roles

in Janino

context.get("login")
context.get("password")
context.get("userId")
context.get("roles")

This specialized scripted tag makes it very easy to conditionally show parts of an interface according to the credentials of a user.

For example:

<!--V 'OGNL:ROLEUSER:role1'-->User is not in role "admin"<!--/V-->
<!--V 'OGNL:ROLEUSER:login1'-->User in named "moderator"<!--/V-->

<!--B 'OGNL:ROLEUSER:role1:[[ isInRole("admin") ]]'-->
    User is in role "admin"
<!--/B-->

<!--B 'OGNL:ROLEUSER:login1:[[ #login == "moderator" ]]'-->
    User is named "moderator"
<!--/B-->

[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:

<!--V 'OGNL:CONFIG:bool'>DISPLAY_BLOCK is false<!--/V-->

<!--V 'OGNL:CONFIG:string'>STRING_VALUE is 'do not match'<!--/V-->

<!--B 'OGNL:CONFIG:bool:[[ getBool("DISPLAY_BLOCK") ]]-->
    DISPLAY_BLOCK is true
<!--/B-->

<!--B 'OGNL:CONFIG:string:[[ getString("STRING_VALUE") != "do not match" ]]-->
    STRING_VALUE is not 'do not match'
<!--/B-->

[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]



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators