The MEL supports all the key syntactic constructions: variables, operations, loop statements, etc.
The language itself is entirely lower case.
The following examples are based on a result feed named persons that has an entry with the following metas:
• name: roger
• age: 42
• ${feeds["person"].metas["name"]}: roger, bruce
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.
• ${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.
${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
• Operations and functions: ${math:max(${my.age}, ${your.age}) + 10}
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}
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}
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.
• You are ${if page.params["n"] != 42} wrong ${elseif math:sum(1,1) ==3} wrong ${elseif math:sum(1,1) == 2} happy ${else} wrong ${/if}
Foreach loop statements
Foreach loops enable you to evaluate a sequence of statements multiple times.
${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}
Important: We recommend declaring whether flags must be used or not specifically, with the + and - signs. For example: !+h or !+h-x Without the + and - signs, the default behavior is inverted.
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.