Skip to main content

ErlyDTL Overview

ErlyDTL is a powerful and flexible template engine written in Erlang.

It provides a simple and intuitive way to generate dynamic content by separating the presentation layer from the business logic. With ErlyDTL, developers can create reusable templates and easily integrate them into their applications.

History

ErlyDTL was originally inspired by Django's template engine and was developed by Evan Miller in 2009. It was designed to be fast, efficient, and easy to use, making it a popular choice among Erlang developers. Since then, the framework has evolved and gained popularity within the Erlang community.

Features

1. Template Inheritance

One of the key features of ErlyDTL is template inheritance. This feature allows developers to create a base template that defines the overall structure of the page and then extend or override specific blocks of content in child templates. This enables the creation of reusable templates and promotes code organization and reusability.

Here's an example of template inheritance in ErlyDTL:

%%% base_template.dt
<html>
<head>
<title>{% block title %}Default Title{% endblock %}</title>
</head>
<body>
{% block content %}Default Content{% endblock %}
</body>
</html>

%%% child_template.dt
{% extends "base_template.dt" %}

{% block title %}Custom Title{% endblock %}

{% block content %}
<h1>Welcome to ErlyDTL</h1>
<p>This is custom content for the child template.</p>
{% endblock %}

In this example, the child template extends the base template and overrides the title block with a custom value. It also overrides the content block with custom content.

2. Template Variables

ErlyDTL allows the use of template variables to dynamically render content. Variables can be defined within the template or passed in from the application code.

Here's an example of using template variables in ErlyDTL:

%%% template.dt
<p>Hello, {{ name }}!</p>

%%% Erlang code
Variables = [{name, "John"}],
ErlyDTL:render(template, Variables).

In this example, the name variable is passed into the template from the Erlang code. The variable is then rendered within the template using double curly braces {{ }}.

3. Conditional Statements

ErlyDTL also supports conditional statements, allowing developers to control the flow of the template based on certain conditions. This feature is useful for rendering different content based on the values of variables or other conditions.

Here's an example of using conditional statements in ErlyDTL:

%%% template.dt
{% if age < 18 %}
<p>You are underage.</p>
{% else %}
<p>You are an adult.</p>
{% endif %}

%%% Erlang code
Variables = [{age, 25}],
ErlyDTL:render(template, Variables).

In this example, the template renders a different message based on the value of the age variable. If the age is less than 18, it displays a message indicating that the person is underage; otherwise, it displays a message indicating that the person is an adult.

4. Iteration

ErlyDTL provides the ability to iterate over lists or other iterable objects within the template. This allows for the dynamic rendering of repetitive content, such as generating HTML tables or lists.

Here's an example of using iteration in ErlyDTL:

%%% template.dt
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>

%%% Erlang code
Variables = [{items, ["Apple", "Banana", "Orange"]}],
ErlyDTL:render(template, Variables).

In this example, the template iterates over the items list and renders each item as an HTML list item (<li>).

Examples

Example 1: Generating an HTML Table

Suppose we have a list of users, and we want to generate an HTML table to display their information.

%%% template.dt
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
</tr>
{% endfor %}
</tbody>
</table>

%%% Erlang code
Users = [
#{name => "John Doe", email => "john@example.com"},
#{name => "Jane Smith", email => "jane@example.com"}
],
Variables = [{users, Users}],
ErlyDTL:render(template, Variables).

In this example, the template iterates over the users list and generates an HTML table with the user's name and email.

Example 2: Conditional Rendering

Let's say we want to render a message based on the user's subscription status.

%%% template.dt
{% if user.subscribed %}
<p>Welcome, {{ user.name }}! You are subscribed.</p>
{% else %}
<p>Welcome, {{ user.name }}! Please subscribe to access premium content.</p>
{% endif %}

%%% Erlang code
User = #{name => "John Doe", subscribed => true},
Variables = [{user, User}],
ErlyDTL:render(template, Variables).

In this example, the template renders a different message based on the value of the subscribed field in the user variable.

Conclusion

ErlyDTL is a powerful and flexible template engine for Erlang that allows for the separation of business logic and presentation layer. It provides features such as template inheritance, template variables, conditional statements, and iteration, making it easy to generate dynamic content. With its simplicity and efficiency, ErlyDTL is a valuable tool for Erlang developers.

To learn more about ErlyDTL, you can visit the official website.