Simple variables
A variable represents a specific data item, or value, and acts as
a placeholder for that value. When a formula encounters a variable, the
formula searches for the value of the variable and uses it in the formula.
The basic syntax is: ${my.variable}
Table 1. Simple expression examples
Expression
|
Result
|
hello ${feeds["person"].metas["name"]}
|
hello roger
|
hello ${feeds["person"].metas["unknown_meta"]}
|
hello
|
hello \${feeds["person"].metas["name"]}
|
hello ${feeds["person"].metas["name"]}
|
Fallbacks
You can declare variable fallbacks using pipes |
-
${my.variable|other.variable}
-
${my.variable|other.variable|last.variable}
-
${my.variable|"hardcoded fallback text"}
Table 2. Fallback/Advanced expression examples
Expression
|
Result
|
hello ${feeds["person"].metas["name"]|"world"}
|
hello roger
|
hello ${feeds["person"].metas["unknown_meta"]|"world"}
|
hello world
|
hello ${feeds["person"].metas["unknown_meta"]|feeds["person"].metas["name"]|"world"}
|
hello roger
|
hello ${feeds["person"].metas["unknown_meta"]|feeds["person"].metas["unknown_meta2"]|"world"}
|
hello world
|
hello ${feeds["person"].metas["unknown_meta"]|feeds["person"].metas["unknown_meta2"]}
|
hello
|
Dynamic variables and functions
Dynamic variables are variables whose values are determined when the program is run.
Table 3. Dynamic expression examples
Expression
|
Result
|
${fn:${feeds["person"].metas["name"]}(${feeds["person"].metas["age"]})}
= ${fn:roger(42)}
|
result of the function roger
passing the parameter 42
|
Operations
-
Strings: ${my.age == "42"}
-
Supported numerical operators: - + * / % == != < > ( )
^ >= <= Example: ${(2+3)*4} = 20
-
Supported string operators: == !=
-
Logical operators: ${(2 <= 3 && 42 != 21) || 3 >=
3} where && = and || =
OR
-
Brackets support:
-
${page.params["q"][1]}
-
${math:sum(1,1)[0]}
-
Concatenation and sum: + is the concatenation operator (as in Java)
and also the sum operator for numerical operations.
-
${"5" + "6"} --> 11
-
${"a" + "b"} --> ab
-
${"a" + "5"} --> " (it will be resolved to NaN+5)
-
${str:join(","a","5")} --> a5
Table 4. Operation examples
Expression
|
Result
|
i am ${feeds["person"].metas["age"]}
|
i am 42
|
i am ${feeds["person"].metas["age"]
+ 10}
|
i am 52
|
i am ${feeds["person"].metas["age"]
/ 2}
|
i am 21
|
i am ${feeds["person"].metas["age"]
% 40}
|
i am 2
|
i am ${feeds["person"].metas["age"]
* 2}
|
i am 84
|
i am ${feeds["person"].metas["age"]
- 20}
|
i am 22
|
i am ${feeds["person"].metas["age"]
+ 10.0}
|
i am 52.0
|
i am ${feeds["person"].metas["age"]
/ 2.0}
|
i am 21.0
|
i am ${feeds["person"].metas["age"]
% 40.0}
|
i am 2.0
|
i am ${feeds["person"].metas["age"]
* 2.0}
|
i am 84.0
|
i am ${feeds["person"].metas["age"]
- 20.0}
|
i am 22.0
|
i am ${feeds["person"].metas["age"]
== "42"}
|
i am true
|
i am ${feeds["person"].metas["age"]
== "24"}
|
i am
|
i am ${feeds["person"].metas["age"]
!= "42"}
|
i am
|
i am ${feeds["person"].metas["age"]
!= "24"}
|
i am true
|
i am ${feeds["person"].metas["age"]
== "roger"}
|
i am true
|
i am ${feeds["person"].metas["age"]
== "robert"}
|
i am
|
i am ${feeds["person"].metas["age"]
!= "roger"}
|
i am
|
i am ${feeds["person"].metas["age"]
!= "robert"}
|
i am true
|
${feeds["person"].metas["age"][0]}
|
roger
|
${feeds["person"].metas["age"][1]}
|
bruce
|
${feeds["person"].metas["age"][1][0]}
|
b
|
${feeds["person"].metas["age"][1][2]}
|
u
|
${"world"[1]}
|
o
|
${("hello" + "world")[8]}
|
l
|
Table 5. Logical operation examples
Expression
|
Result
|
I am ${Roger && 42}
|
false
|
I am ${Roger || 42}
|
true
|
I am ${Roger || (42 && Roger)}
|
false
|
Functions
MEL Functions are classified by categories under the following namespaces:
-
category functions: className , url .
-
currency: format .
-
date: addDays , addYears , addMilliseconds ,
addMonths , addMinutes , addHours ,
addSeconds , addWeeks , now ,
format , elapsedTime .
-
entry: cleanId , previewHtmlUrl , thumbnailUrl ,
downloadUrl , previewImageUrl .
-
list: join , size .
-
math: min , atan , max ,
pow , asin , cos , ceil ,
sqrt , random , log2 ,
sum , round , divide ,
multiply , log , subtract ,
exp , abs , floor , sin ,
avg , tan , acos .
-
number: format .
-
str (string): replace , lowerCase ,
abbreviate , upperCase , length ,
hashCode , split , contains ,
trim , substr .
-
var: get , set .
Note:
The fn namespace is used as the standard namespace.
Function calls:
-
Simple: ${date:now()}
-
With arguments: ${str:substr("toto", 2)}
-
With variable arguments: ${math:max(${my.age}, ${your.age})}
Table 6. Function expression examples
Expression
|
Result
|
I am ${math:substr(page.params["name"],
0, 3)}
|
I am rog
|
${number:format(0.25, 1, 4, 1, 1, true, "HALF_UP")}
|
0.3
|
Internationalization functions
-
${user:locale(MELHttpServletRequest request)} returns
the locale used by the current user from the HTTP query.
-
${date:format(MELString date, MELString pattern, MELString locale,
MELString timeZone)} returns the date formatted as the output
pattern using the specified locale and timezone.
-
${i18n:message(MELHttpServletRequest request, MELType code)
translates a string like ${i18n[code]} but using the
locale from the HTTP query.
-
${i18n:message(MELHttpServletRequest request, MELType code,
MELType[]... parameters) translates a string containing variable
parts (specified by {0} , {1} , etc.)
which are replaced by the values in parameters.
Sample usage:
${user:locale(request)}
> en
${date:format(${entry.metas['date']}, 'dd/MM/yyyy hh:mm:ss', 'en_EN', 'GMT')
> 10/02/2014 12:30:10
${date:format(${entry.metas['date']}, "MM/dd/yyyy hh:mm:ss", "en_US", "GMT-5")}
> 02/10/2014 07:30:10
In localization file (*.properties ):
error.message=Error code: {0}
document.summary=created by {0} on {1} at {2}
In the Mashup Builder:
${i18n:message(request, 'error.message', '404')}
> Error code: 404
${i18n:message(request, 'document.summary', author, date, time)}
> created by John Doe on February 10, 2014 at 10:30am
Combinations
Ternaries
-
Simple: ?{true?yes:no}
-
Advanced: ?{${persons.age}?i am ${persons.age / 2}:age not found}
-
Short syntax: ?{true?:no}
-
Nested: ?{true?{true?nested yes:nested no}:no}
Table 7. Ternary expression examples
Expression
|
Result
|
?{feeds["person"].metas["name"]?meta exists:meta not found}
|
meta exists
|
?{feeds["person"].metas["unknown_meta"]?meta exists:meta not found}
|
meta not found
|
?{feeds["person"].metas["age"]?i am feeds["person"].metas["age"]/2:age not found}
|
i am 21
|
?{true?yes:no}
|
yes
|
?{yes:no}
|
no
|
?{true?:no}
|
-
|
?{true?{true?nested yes:nested no}:no}
|
nested yes
|
?{true?{?nested yes:nested no}:no}
|
nested no
|
${if feeds["person"].metas["name"] !=
"roger"} wrong ${elseif math:sum(1,1) ==3} wrong ${elseif math:sum(1,1)
== 2} happy ${else} wrong ${/if}
|
happy
|
Table 8. Special ternary expression examples
Expression
|
Result
|
hello ${page.params["name"]}
|
hello roger
|
hello ${page.params["unknown_meta"]}
|
hello
|
hello ?{page.params["name"]?no otherwise}
|
hello no otherwise
|
hello ?{page.params["unknown_meta"]?no
otherwise}
|
-
|
If statements
The If statement enables you to evaluate a sequence
of statements if a condition is true and evaluate a different sequence
of statements if it is not true.
Foreach loop statements
Foreach loops enable you to evaluate a sequence of
statements multiple times.
-
${foreach 1,2,3} ${loop.element}?{$loop.hasNext}?,} ${/foreach}
go!
-
${foreach number in 1,2,3} ${number}?{${loop.hasNext}?,} ${/foreach}
go!
Table 9. Loop expression examples
Expression
|
Result
|
${foreach page.params["name"]}I am ${loop.element}${/foreach}
|
I am roger
|
${foreach "a", "b", "c"}${loop.element}${/foreach}
|
abc
|
${foreach 1,2,3}and ${loop.index} ${/foreach}
|
and 0 and 1 and 2
|
${foreach 1,2,3}and ${loop.element}
${/foreach}
|
and 1 and 2 and 3
|
${foreach number in 1,2,3} and ${number}
${/foreach}
|
and 1 and 2 and 3
|
Flags
-
!m --> HTML escaping
-
!x --> XML escaping. For example: ${entry.metas['text']!+x}
-
!u --> URL encoding. This flag is useful to declare
URLs.
-
!h --> Highlighting (only available for metas).
For example: ${entry.metas["title"]!h}
Code samples
The two following code samples using MEL can be pasted in an HTML
widget. They both contain typical iteration made with foreach
statements.
<h1>Iterate over entries</h1>
<ul>
${foreach hit in feed.entries }
<li>
<h1>${hit.title}</h1>
<a href="${entry:downloadUrl(hit, request, '<Page name>')}">Download</a>
<iframe src="${entry:previewHtmlUrl(hit, request, '<Page name>')}"></iframe>
<img src="${entry:previewImageUrl(hit, request, '<Page name>')}" />
</li>
${/foreach}
</ul>
<h1>Iterate over the author facet</h1>
<ul>
${foreach catElement in feeds[’cloudview’].facets[’author’].leaves}
<li>
<a href="${category:url(catElement, request, feeds[’cloudview’])}"
class="${category:className(catElement)}">
${i18n[catElement]}
</a>
</li>
${/foreach}
</ul>
Note:
request refers to the request variable available in any context.
|