Last active
June 25, 2021 11:21
-
-
Save l-gu/3adb395b916d20a4d540ec84c295db6b to your computer and use it in GitHub Desktop.
Velocity Language Cheat Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## ---------------------------------------------------------------------- | |
## Telosys Template : Velocity Cheat Sheet | |
## ---------------------------------------------------------------------- | |
#* | |
Multi-lines comment | |
*# | |
#[[ | |
Unparsed block (all this block will be rendered as is) | |
$undefined | |
#set($i = 50) | |
#foobar #zzz(ee) | |
#include("foo.txt") | |
]]# | |
## ---------------------------------------------------------------------- | |
## SET | |
## ---------------------------------------------------------------------- | |
#set($i = 50) | |
i value : $i | |
#set($f = 12.34) | |
f value : $f | |
#set($b = true) | |
b value : $b | |
#set($s = "abc") | |
s value : $s | |
#set($s = "abc $i")## 'i' variable is replaced by its value | |
s value : $s | |
#set($s = 'abc $i')## no variable substitution | |
s value : $s | |
#set($s = | |
'line 1 $a | |
line 2 $b | |
line 3 $c' )## 3 lines | |
s lines : | |
$s | |
## #set( $v = null ) ## invalid syntax | |
## --- Arithmetic operators | |
#set($x = 12) | |
#set($y = 4) | |
#set ( $r = $x + $y + 1 ) r : $r | |
#set ( $r = $x - $y ) r : $r | |
#set ( $r = $x * $y ) r : $r | |
#set ( $r = $x / $x ) r : $r | |
#set ( $r = $x % 5 ) r : $r | |
## ---------------------------------------------------------------------- | |
## IF / ELSE / ELSEIF | |
## ---------------------------------------------------------------------- | |
#if ( $b ) | |
b is true | |
#else | |
b is false | |
#end | |
b is #if( $b )TRUE#{else}FALSE#{end}, yes it is! | |
#set($v = 2) | |
#if ( $v == 1 ) | |
v equals 1 | |
#elseif ( $v == 2 ) | |
v equals 2 | |
#else | |
v other | |
#end | |
#set($s = "#if( $b )TRUE#{else}FALSE#{end}" ) | |
s : $s ## s : "TRUE" | |
## --- Comparison operators and logical operators | |
#set($x = 12.34) #set ($y = 56.78 ) x : $x, y : $y | |
#if ( $x == $y )#end | |
#if ( $x eq $y )#end | |
#if ( $x != $y )#end | |
#if ( $x ne $y )#end | |
#if ( $x > $y )#end | |
#if ( $x < $y )#end | |
#if ( $x <= $y )#end | |
#if ( ! ($x == $y) )#end | |
#if ( not ($x == $y) )#end | |
#if ( $x == $y && $x > 10 )#end | |
#if ( $x == $y || $x > 10 )#end | |
#if ( $x == $y || ( $x > 10 && $s < 200 ) )#end | |
#set($list = ["a","b","c"]) | |
#if ( $list.isEmpty() ) | |
list is empty | |
#else | |
list is not empty | |
#end | |
## ---------------------------------------------------------------------- | |
## FOREACH | |
## ---------------------------------------------------------------------- | |
#foreach( $item in [1..4] ) | |
. ${item} | |
$foreach.class.name | |
count : $foreach.count / index : $foreach.index | |
hasNext : $foreach.hasNext / hasNext() : $foreach.hasNext() | |
first : $foreach.first / isFirst() : $foreach.isFirst() | |
last : $foreach.last / isLast() : $foreach.isLast() | |
#set($n = $foreach.count )## global variable | |
#end | |
n (after foreach ) : $n | |
## ---------------------------------------------------------------------- | |
## MACRO | |
## ---------------------------------------------------------------------- | |
#macro( add $a1 $a2 ) | |
#set ( $r = $a1 + $a2 ) | |
Add $a1 + $a2 : result is $r | |
#end | |
## call macro : | |
#add( 20, 3 ) | |
## ---------------------------------------------------------------------- | |
## DEFINE | |
## ---------------------------------------------------------------------- | |
#define( $header ) | |
// My super project | |
// Copyright 2018 | |
// Author $author | |
#end | |
#set( $author = "John" ) | |
$header | |
## ---------------------------------------------------------------------- | |
## EVALUATE | |
## ---------------------------------------------------------------------- | |
## evaluate the instruction string | |
#evaluate('#set($z=12)') | |
z : $z | |
## init map content as string : | |
#set( $map = '{ "k1":"v1" , "k2":"v2" }' ) | |
map : $map | |
## compose instruction string : | |
#set( $instruction = '#set( $z = ' + $map + ' )' ) | |
instruction : $instruction | |
## evaluate instruction string : | |
#evaluate($instruction) | |
z : $z ## z is a map instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment