Keywords: Mustache templates | conditional rendering | JavaScript preprocessing
Abstract: This article provides an in-depth exploration of two core approaches for implementing conditional rendering in Mustache's logic-less templates: preprocessing data with JavaScript to set flags, and utilizing Mustache's inverted sections. Using notification list generation as a case study, it analyzes how to dynamically render content based on notified_type and action fields, while comparing Mustache with Handlebars in conditional logic handling, offering practical technical solutions for developers.
Conditional Logic Implementation Mechanisms in Mustache
Mustache, as a logic-less template engine, emphasizes the separation of business logic from the view layer in its design philosophy. This design brings template simplicity and maintainability, but also presents challenges for conditional rendering. This article systematically analyzes two main methods for implementing conditional logic in Mustache, based on the typical case of notification list generation.
JavaScript Preprocessing Method
The most direct approach is to preprocess data through JavaScript before passing it to the template. For the given notification object:
[
{
"id": 1364,
"read": true,
"author_id": 30,
"author_name": "Mr A",
"author_photo": "image.jpg",
"story": "wants to connect",
"notified_type": "Friendship",
"action": "create"
}
]
We can set corresponding flags in JavaScript based on the notified_type and action fields:
// Preprocess data
notifications.forEach(function(notification) {
if(notification.notified_type == "Friendship") {
notification.type_friendship = true;
} else if(notification.notified_type == "Other" && notification.action == "invite") {
notification.type_other_invite = true;
}
// Additional conditions can be added
});
In the Mustache template, we can use section syntax for conditional rendering:
{{#type_friendship}}
<div class="friendship-notification">
<p>Friendship Request Notification</p>
</div>
{{/type_friendship}}
{{#type_other_invite}}
<div class="invite-notification">
<p>Invitation Notification</p>
</div>
{{/type_other_invite}}
Alternative Approach with Inverted Sections
Mustache provides inverted section functionality that renders content when conditions are false. This mechanism is based on Mustache's handling of boolean values: inverted sections are rendered when a key doesn't exist, is false, or is an empty list.
{{#value}}
Display when value is true
{{/value}}
{{^value}}
Display when value is false
{{/value}}
However, this method has limitations when dealing with complex conditions, especially when multiple fields need to be checked simultaneously.
Comparison with Handlebars
For scenarios requiring more complex conditional logic, Handlebars offers better solutions. Handlebars is fully compatible with Mustache syntax but adds features like helper functions, allowing more complex logic implementation within templates:
{{#if (eq notified_type "Friendship")}}
<!-- Render friendship notification -->
{{else if (and (eq notified_type "Other") (eq action "invite"))}}
<!-- Render invitation notification -->
{{/if}}
The advantage of this approach is that it keeps some logic at the template level, reducing the complexity of JavaScript preprocessing code.
Best Practice Recommendations
In actual development, it's recommended to choose appropriate methods based on project requirements:
- For simple conditional rendering, use JavaScript preprocessing combined with Mustache section syntax
- For scenarios requiring template independence, consider using Handlebars
- Avoid embedding complex business logic in templates to maintain template simplicity
- Unify conditional processing strategies to ensure code maintainability
Through proper data preprocessing and template design, flexible conditional rendering functionality can be achieved while maintaining Mustache's simplicity.