DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Enhancing Mustache via an If-Statement With El Expressions

Enhancing Mustache via an If-Statement With El Expressions

Johannes Neubauer shares his success at stretching a template language (Mustache) beyond the design choices of its creators. Code snippets included.

Johannes Neubauer user avatar by
Johannes Neubauer
·
Jul. 15, 16 · Java Zone · Tutorial
Like (1)
Save
Tweet
15.51K Views

Join the DZone community and get the full member experience.

Join For Free

Mustache is a well-adopted template language which itself says that it is logic-less. So, there are pseudo-logic statements, which allow to iterate over a list or do a null-check, and it is possible to add arbitrary (unparameterized) template function. But they make a hold when it comes to an if-statement with a boolean check.

There is a lot of discussion going on in the net on why a null-check and template functions are ok, but an if-statement and parameters are not… but hey we have template functions, so why not add our own implementation of such things?

Long story short: I added a gist (my first, yay) implementing this using EL expressions. So this is a solution for mustache templates evaluated either in Java SE applications, in the backend of a Java EE application, or alike. I let it as a homework to the gentle reader to implement this in JavaScript using eval() for evaluating the boolean expression in the if-statement. The solution allows to write something like this as a mustache template:

a is {{#if}}(a <= 3)not {{/if}}greater than three.
b is {{#if}}(b <= (4 + 1))not {{/if}}greater than 5.

And give it a == 100 as well as b == 5. The output is as follows:

a is greater than three. b is not greater than 5.

Short after doing this heroic first step to a logic-full mustache, I was not quite satisfied with my solution, because the syntax is very ugly to write and to understand. But mustache has the limitation that start and end tag of an template have to be the same. The only other option would have been to write something like this (without changing the mustache parser):


a is {{#if (a <= 3)}}not {{/if (a <= 3)}}greater than three.
b is {{#if (b <= (4 + 1))}}not {{/if (b <= (4 + 1))}}greater than 5.

After a second internet research I found the follow-up template language handlebars, which has block-expressions (allowing parameters) and corresponding helper functions. With this you can end up with a nice syntax like:

a is {{#if (a <= 3)}}not {{/if}}greater than three.
b is {{#if (b <= (4 + 1))}}not {{/if}}greater than 5.

Summary: Handlebars rules.

Exciting.

Mustache (template system)

Published at DZone with permission of Johannes Neubauer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Is Java Still Relevant?
  • Your Old Laptop Is Your New Database Server
  • Password Authentication: How to Correctly Do It
  • How to Generate Fake Test Data

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo