- Introduction
- Displaying Data
- HTML Escaping
- Comments
- Variables
- Expressions
- Operators
- Member Access
- Array Access
JHP templates are plain text files with special syntax for dynamic content. They typically have a .jhp extension and can contain HTML, text, or any other format.
You can display data by wrapping variables in double curly braces:
<h1>Hello, {{ name }}!</h1>
<p>You have {{ count }} messages.</p>The {{ }} syntax automatically escapes HTML to prevent XSS attacks.
By default, all output is HTML-escaped:
{{ userInput }}If userInput contains <script>alert('xss')</script>, it will be rendered as:
<script>alert('xss')</script>To output raw HTML without escaping, use triple curly braces:
{{{ htmlContent }}}Example:
ctx.add("htmlContent", "<b>Bold Text</b>");Escaped: {{ htmlContent }}
<!-- Output: <b>Bold Text</b> -->
Raw: {{{ htmlContent }}}
<!-- Output: <b>Bold Text</b> -->Warning: Only use raw output with trusted content to avoid XSS vulnerabilities.
You can disable HTML escaping globally in settings:
Settings settings = Settings.builder()
.escape(false)
.build();JHP doesn't have built-in comment syntax, but you can use HTML comments:
<!-- This is a comment -->
{{ name }}Variables are accessed by name:
{{ username }}
{{ age }}
{{ isActive }}Variables must be added to the Context before rendering:
Context ctx = new Context();
ctx.add("username", "Alice");
ctx.add("age", 25);
ctx.add("isActive", true);JHP supports various types of expressions:
{{ "Hello World" }}
{{ 42 }}
{{ 3.14 }}
{{ true }}
{{ false }}
{{ null }}{{ "Hello " + name }}
{{ firstName + " " + lastName }}{{ 5 + 3 }} <!-- 8 -->
{{ 10 - 4 }} <!-- 6 -->
{{ 6 * 7 }} <!-- 42 -->
{{ 20 / 4 }} <!-- 5.0 -->
{{ 17 % 5 }} <!-- 2.0 -->
{{ 2 + 3 * 4 }} <!-- 14 (respects precedence) -->
{{ (2 + 3) * 4 }} <!-- 20 -->{{ age >= 18 ? "Adult" : "Minor" }}
{{ count > 0 ? count + " items" : "No items" }}{{ x > y }} <!-- Greater than -->
{{ x < y }} <!-- Less than -->
{{ x >= y }} <!-- Greater than or equal -->
{{ x <= y }} <!-- Less than or equal -->
{{ x == y }} <!-- Equal -->
{{ x != y }} <!-- Not equal -->{{ isActive && isVerified }} <!-- AND -->
{{ isAdmin || isModerator }} <!-- OR -->
{{ !isDeleted }} <!-- NOT -->{{ -value }} <!-- Negation -->
{{ !condition }} <!-- Logical NOT -->Access object properties using dot notation:
{{ user.name }}
{{ user.email }}
{{ product.price }}Java Context:
Map<String, Object> user = new HashMap<>();
user.put("name", "Alice");
user.put("email", "alice@example.com");
ctx.add("user", user);{{ user.address.city }}
{{ order.customer.name }}Access array or list elements using square brackets:
{{ items[0] }}
{{ items[1] }}
{{ items[index] }}Java Context:
List<String> items = Arrays.asList("Apple", "Banana", "Cherry");
ctx.add("items", items);
ctx.add("index", 1);{{ config["database"] }}
{{ settings["theme"] }}You can combine multiple operators and access methods:
{{ (user.age >= 18 && user.verified) ? "Access Granted" : "Access Denied" }}
{{ items[0].name + " - $" + items[0].price }}
{{ (total * 0.9).toFixed(2) }}Call functions within expressions:
{{ touppercase(name) }}
{{ len(items) }}
{{ trim(userInput) }}
{{ touppercase(user.name) + " (" + len(user.email) + ")" }}See Functions for a complete list of available functions.
JHP preserves whitespace in templates:
<p>
{{ name }}
</p>This will include the newlines and indentation in the output.
- Learn about Control Structures
- Explore Functions
- Understand Context & Variables