Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
Velocity Templates Every day Features

Velocity Templates Every day Features

Posted on March 23, 2025March 24, 2025 by Toma Velev

Velocity Templates are a powerful templating engine that allows outputting custom content in your format with most basic case – HTML. I’ve written very basic example with Spring Boot. Here are some everyday features of Velocity templates that you could use:

Variables

You can define variables in the template and use them throughout the page.

#set($name = "John")
Hello, $name!

Loops

Velocity supports various types of loops, such as #foreach for iterating over arrays or collections.

#foreach($item in $items)
  * $item
#end

Conditional statements

You can use #if to check conditions and display different content based on the result.

#if($isAdmin)
  You are an administrator.
#else
  You are not an administrator.
#end

Functions

Velocity allows you to define custom functions that can be used within templates.

#define(greet $name)
  Hello, $name!
#end

${greet("John")}

Include directives

You can include other Velocity templates using the #include directive, making it easy to reuse code.

#include("header.vm")
  <!-- content here -->
#include("footer.vm")

Escape sequences

Velocity supports various escape sequences for displaying special characters, such as #lt for less than and #gt for greater than.

Hello, <b>John</b>!

URL encoding

You can use the $esc.html() function to URL encode strings, making it easier to pass data in URLs.

<a href="http://example.com?name=${esc.html("John")}">Click me!</a>

Date and time formatting

Velocity itself does not have built-in date formatting, but you can format dates using SimpleDateFormat

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd")
String formattedDate = formatter.format(new Date());

Math operations

You can perform basic math operations, such as addition and multiplication, using the $num function.

The sum of 2 and 3 is: ${num.add(2, 3)}

String manipulation

Velocity offers various string functions, such as trim() and replace(), for manipulating strings.

The trimmed name is: ${esc.html("   John   ").trim()}

Array manipulation

You can use Velocity’s array functions, such as sort() and get(), to manipulate arrays.

The sorted array is: ${array.sort([3, 1, 2])}

Object manipulation

Velocity provides various object functions, such as get() and set(), for manipulating objects.

The value of the "name" property is: ${obj.get("name")}

Custom tags

You can define custom tags using the #tag directive, making it easy to create reusable UI components.

#tag(name="greeting", body="Hello, $name!")
${greeting("John")}

Velocity macros

Velocity allows you to define custom macros that can be used within templates, making it easy to reuse code.

#define(greet $name)
  Hello, $name!
#end

${greet("John")}

Error handling

Velocity provides various error-handling mechanisms, such as the #error directive and the $esc.html() function, to help you handle errors.

#if($error)
  An error occurred: ${esc.html($error)}
#else
  No errors occurred.
#end

These are just a few examples of the many features available in Velocity templates. By mastering these features, you can create powerful and flexible web applications.

  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)