Skip to content

Latest commit

 

History

History
270 lines (198 loc) · 4.93 KB

File metadata and controls

270 lines (198 loc) · 4.93 KB

Template Syntax

Introduction

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.

Displaying Data

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.

HTML Escaping

Escaped Output (Default)

By default, all output is HTML-escaped:

{{ userInput }}

If userInput contains <script>alert('xss')</script>, it will be rendered as:

&lt;script&gt;alert('xss')&lt;/script&gt;

Raw Output

To output raw HTML without escaping, use triple curly braces:

{{{ htmlContent }}}

Example:

ctx.add("htmlContent", "<b>Bold Text</b>");
Escaped: {{ htmlContent }}
<!-- Output: &lt;b&gt;Bold Text&lt;/b&gt; -->

Raw: {{{ htmlContent }}}
<!-- Output: <b>Bold Text</b> -->

Warning: Only use raw output with trusted content to avoid XSS vulnerabilities.

Disabling Escaping Globally

You can disable HTML escaping globally in settings:

Settings settings = Settings.builder()
    .escape(false)
    .build();

Comments

JHP doesn't have built-in comment syntax, but you can use HTML comments:

<!-- This is a comment -->
{{ name }}

Variables

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);

Expressions

JHP supports various types of expressions:

Literals

{{ "Hello World" }}
{{ 42 }}
{{ 3.14 }}
{{ true }}
{{ false }}
{{ null }}

String Concatenation

{{ "Hello " + name }}
{{ firstName + " " + lastName }}

Arithmetic

{{ 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 -->

Ternary Operator

{{ age >= 18 ? "Adult" : "Minor" }}
{{ count > 0 ? count + " items" : "No items" }}

Operators

Comparison Operators

{{ 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 -->

Logical Operators

{{ isActive && isVerified }}     <!-- AND -->
{{ isAdmin || isModerator }}     <!-- OR -->
{{ !isDeleted }}                 <!-- NOT -->

Unary Operators

{{ -value }}         <!-- Negation -->
{{ !condition }}     <!-- Logical NOT -->

Member Access

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);

Nested Access

{{ user.address.city }}
{{ order.customer.name }}

Array Access

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);

Map Access

{{ config["database"] }}
{{ settings["theme"] }}

Complex Expressions

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) }}

Function Calls

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.

Whitespace Handling

JHP preserves whitespace in templates:

<p>
    {{ name }}
</p>

This will include the newlines and indentation in the output.

Next Steps