Apache Velocity

Apache Velocity first released in April 2001, is a Java-based template engine that provides a template language to reference objects defined in Java code. It aims to ensure clean separation between the presentation tier and business tiers in a Web application (the model–view–controller design pattern).

Apache Velocity
Developer(s)Apache Software Foundation
Stable release
2.1 / March 31, 2019 (2019-03-31)
RepositoryVelocity Repository
Written inJava
Operating systemCross-platform
Typetemplate engine
LicenseApache License 2.0
Websitevelocity.apache.org

Velocity is an open source software project hosted by the Apache Software Foundation. It is released under the Apache License. Jon Scott Stevens derived the name from the AltiVec Velocity Engine in the PowerPC G4 chip.

Uses

Some common types of applications that use Velocity are:

  • AWS API Gateway body mapping template.
    Web applications: Web developers render HTML page structures. The content is populated with dynamic information. The page is processed with VelocityViewServlet or any of a number of frameworks that support Velocity.
  • Source code generation: Velocity can be used to generate Java, SQL, or PostScript source code, based on templates. A number of open source and commercial development software packages use Velocity in this manner.[1]
  • Automatic emails: Many applications generate automatic emails for account signup, password reminders, or automatically sent reports. Using Velocity, the email template can be stored in a text file, rather than directly embedded in Java code.
  • XML transformation: Velocity provides an Ant task, called Anakia, which reads an XML file and makes it available to a Velocity template. A common application is to convert software documentation stored in a generic "xdoc" format into a styled HTML document.
  • AWS API Gateway uses Apache Velocity engine for body mapping templates - JSON is output.

Code examples

The following template:

## Velocity Hello World
<html>
    <body>
        #set( $foo = "Velocity" )
        ## followed by
        Hello $foo World!
    </body>
</html>

processed by Velocity produces the following HTML:

<html>
    <body>
        Hello Velocity World!
    </body>
</html>

Another example using conditions:

#if( $foo < 10 )
    **Go North**
#elseif( $foo == 10 )
    **Go East**
#elseif( $bar == 6 )
    **Go South**
#else
    **Go West**
#end

For AWS API Gateway body mapping template, for DynamoDB HTTP:

{
    "TableName": "happy-marketer",
    "KeyConditionExpression": "pk = :pk AND begins_with(sk, :sk)",
    #if ($input.params('projectStatus') == 'archived')
    "FilterExpression": "projectStatus = :Archived",
    #else
    "FilterExpression": "projectStatus = :InProgress OR projectStatus = :Completed",
    #end
    "ExpressionAttributeValues": {
        ":pk": {
            "S": "project"
        },
        ":sk": {
            "S": "$context.authorizer.claims.email"
        },
        #if ($input.params('projectStatus') == 'archived')
        ":Archived": {
            "S": "Archived"
        }
        #else
        ":InProgress": {
            "S": "In Progress"
        },
        ":Completed": {
            "S": "Completed"
        }
        #end
    }
}

Another example with optional array; without condition will fail because of invalid resulting JSON:

{
    "email": "$context.authorizer.claims.email"
    #if ($input.params('countries') != "")
    , "countries": $input.params('countries')
    #end
}

The syntax and overall concept of the Apache Velocity templates is similar to the syntax of the older WebMacro template engine, which is now also an open source project.

See also

References

  1. "PoweredByVelocity". Velocity Wiki. Wiki.apache.org. 2009-12-30. Archived from the original on 23 March 2010. Retrieved 2010-03-29.

Bibliography

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.