DZone
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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • OpenAPI: Extend Functionality of Generator Plugin Using Custom Mustache Templates

Trending

  • Build a Voice Assistant With Gemini Live API
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Avoiding AI Debt as a Team
  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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.

By 
Johannes Neubauer
·
Jul. 15, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
24.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • OpenAPI: Extend Functionality of Generator Plugin Using Custom Mustache Templates

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook